Skip to content

Commit

Permalink
Migrate codebase from cachy to poetry.utils.cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
chadac authored and neersighted committed Oct 11, 2022
1 parent e512cbe commit c2b1fb3
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 113 deletions.
98 changes: 49 additions & 49 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ poetry-core = "^1.3.2"
poetry-plugin-export = "^1.1.2"
"backports.cached-property" = { version = "^1.0.2", python = "<3.8" }
cachecontrol = { version = "^0.12.9", extras = ["filecache"] }
cachy = "^0.3.0"
cleo = "^1.0.0a5"
crashtest = "^0.3.0"
dulwich = "^0.20.46"
Expand Down Expand Up @@ -80,6 +79,8 @@ urllib3 = "^1.26.0"
pre-commit = "^2.6"

[tool.poetry.group.test.dependencies]
# Cachy frozen to test backwards compatibility for `poetry.utils.cache`.
cachy = "0.3.0"
deepdiff = "^5.0"
flatdict = "^4.0.1"
httpretty = "^1.0"
Expand Down
11 changes: 2 additions & 9 deletions src/poetry/console/commands/cache/clear.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from poetry.config.config import Config
from poetry.console.commands.command import Command
from poetry.utils.cache import FileCache


class CacheClearCommand(Command):
Expand All @@ -18,8 +19,6 @@ class CacheClearCommand(Command):
options = [option("all", description="Clear all entries in the cache.")]

def handle(self) -> int:
from cachy import CacheManager

cache = self.argument("cache")

parts = cache.split(":")
Expand All @@ -33,13 +32,7 @@ def handle(self) -> int:
except ValueError:
raise ValueError(f"{root} is not a valid repository cache")

cache = CacheManager(
{
"default": parts[0],
"serializer": "json",
"stores": {parts[0]: {"driver": "file", "path": str(cache_dir)}},
}
)
cache = FileCache(cache_dir)

if len(parts) == 1:
if not self.option("all"):
Expand Down
18 changes: 4 additions & 14 deletions src/poetry/repositories/cached.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from typing import TYPE_CHECKING
from typing import Any

from cachy import CacheManager
from packaging.utils import canonicalize_name
from poetry.core.constraints.version import parse_constraint

from poetry.config.config import Config
from poetry.repositories.repository import Repository
from poetry.utils.cache import FileCache


if TYPE_CHECKING:
Expand All @@ -30,17 +30,7 @@ def __init__(
super().__init__(name)
self._disable_cache = disable_cache
self._cache_dir = (config or Config.create()).repository_cache_directory / name
self._cache = CacheManager(
{
"default": "releases",
"serializer": "json",
"stores": {
"releases": {"driver": "file", "path": str(self._cache_dir)},
"packages": {"driver": "dict"},
"matches": {"driver": "dict"},
},
}
)
self._release_cache: FileCache[dict[str, Any]] = FileCache(path=self._cache_dir)

@abstractmethod
def _get_release_info(
Expand All @@ -60,7 +50,7 @@ def get_release_info(self, name: NormalizedName, version: Version) -> PackageInf
if self._disable_cache:
return PackageInfo.load(self._get_release_info(name, version))

cached = self._cache.remember_forever(
cached = self._release_cache.remember(
f"{name}:{version}", lambda: self._get_release_info(name, version)
)

Expand All @@ -73,7 +63,7 @@ def get_release_info(self, name: NormalizedName, version: Version) -> PackageInf
)
cached = self._get_release_info(name, version)

self._cache.forever(f"{name}:{version}", cached)
self._release_cache.put(f"{name}:{version}", cached)

return PackageInfo.load(cached)

Expand Down
30 changes: 13 additions & 17 deletions src/poetry/repositories/legacy_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,19 @@ def _find_packages(
if not constraint.is_any():
key = f"{key}:{constraint!s}"

if self._cache.store("matches").has(key):
versions = self._cache.store("matches").get(key)
else:
page = self._get_page(f"/{name}/")
if page is None:
self._log(
f"No packages found for {name}",
level="debug",
)
return []

versions = [
(version, page.yanked(name, version))
for version in page.versions(name)
if constraint.allows(version)
]
self._cache.store("matches").put(key, versions, 5)
page = self._get_page(f"/{name}/")
if page is None:
self._log(
f"No packages found for {name}",
level="debug",
)
return []

versions = [
(version, page.yanked(name, version))
for version in page.versions(name)
if constraint.allows(version)
]

return [
Package(
Expand Down
Loading

0 comments on commit c2b1fb3

Please sign in to comment.