smartcache-maxximuss 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Your Name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,49 @@
1
+ Metadata-Version: 2.4
2
+ Name: smartcache-maxximuss
3
+ Version: 1.0.0
4
+ Summary: High-performance Python caching library featuring TTL, LRU, async support, and disk/memory backends.
5
+ Author-email: Maxximuss <xxxmaxximussxxx@gmail.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/Maxximuss-hub/Smartcache
8
+ Project-URL: Bug Tracker, https://github.com/Maxximuss-hub/Smartcache/issues
9
+ Project-URL: Author Profile, https://github.com/Maxximuss-hub
10
+ Keywords: cache,caching,lru,ttl,async,performance,smartcache
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.8
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
20
+ Requires-Python: >=3.8
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ Dynamic: license-file
24
+
25
+ # SmartCache 🚀
26
+
27
+ Высокопроизводительная библиотека кэширования для Python.
28
+ **Создана для реальных задач: экономия ресурсов, ускорение API, работа с асинхронными запросами.**
29
+
30
+ [![PyPI](https://img.shields.io/pypi/v/smartcache)](https://pypi.org/project/smartcache/)
31
+ [![Python](https://img.shields.io/badge/Python-3.8%2B-blue)]()
32
+ [![License](https://img.shields.io/badge/License-MIT-green)](LICENSE)
33
+
34
+ ## 💡 Зачем это нужно?
35
+
36
+ Представь, что твой API-запрос стоит $0.01.
37
+ 1000 пользователей одновременно запрашивают погоду → **$10 вместо $0.01**.
38
+ С `SmartCache` ты экономишь **99.9%** запросов!
39
+
40
+ ## 🚀 Установка
41
+
42
+ ```bash
43
+ # Установка из PyPI (когда будет опубликовано)
44
+ pip install smartcache
45
+
46
+ # Или локальная установка (в разработке)
47
+ git clone https://github.com/yourusername/smartcache.git
48
+ cd smartcache
49
+ pip install -e .
@@ -0,0 +1,25 @@
1
+ # SmartCache 🚀
2
+
3
+ Высокопроизводительная библиотека кэширования для Python.
4
+ **Создана для реальных задач: экономия ресурсов, ускорение API, работа с асинхронными запросами.**
5
+
6
+ [![PyPI](https://img.shields.io/pypi/v/smartcache)](https://pypi.org/project/smartcache/)
7
+ [![Python](https://img.shields.io/badge/Python-3.8%2B-blue)]()
8
+ [![License](https://img.shields.io/badge/License-MIT-green)](LICENSE)
9
+
10
+ ## 💡 Зачем это нужно?
11
+
12
+ Представь, что твой API-запрос стоит $0.01.
13
+ 1000 пользователей одновременно запрашивают погоду → **$10 вместо $0.01**.
14
+ С `SmartCache` ты экономишь **99.9%** запросов!
15
+
16
+ ## 🚀 Установка
17
+
18
+ ```bash
19
+ # Установка из PyPI (когда будет опубликовано)
20
+ pip install smartcache
21
+
22
+ # Или локальная установка (в разработке)
23
+ git clone https://github.com/yourusername/smartcache.git
24
+ cd smartcache
25
+ pip install -e .
@@ -0,0 +1,38 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "smartcache-maxximuss"
7
+ version = "1.0.0"
8
+ description = "High-performance Python caching library featuring TTL, LRU, async support, and disk/memory backends."
9
+ readme = "README.md"
10
+ requires-python = ">=3.8"
11
+ license = "MIT"
12
+
13
+ authors = [
14
+ {name = "Maxximuss", email = "xxxmaxximussxxx@gmail.com"}
15
+ ]
16
+
17
+ keywords = ["cache", "caching", "lru", "ttl", "async", "performance", "smartcache"]
18
+ classifiers = [
19
+ "Development Status :: 4 - Beta",
20
+ "Intended Audience :: Developers",
21
+ "Programming Language :: Python :: 3",
22
+ "Programming Language :: Python :: 3.8",
23
+ "Programming Language :: Python :: 3.9",
24
+ "Programming Language :: Python :: 3.10",
25
+ "Programming Language :: Python :: 3.11",
26
+ "Programming Language :: Python :: 3.12",
27
+ "Topic :: Software Development :: Libraries :: Python Modules"
28
+ ]
29
+
30
+ [project.urls]
31
+ "Homepage" = "https://github.com/Maxximuss-hub/Smartcache"
32
+ "Bug Tracker" = "https://github.com/Maxximuss-hub/Smartcache/issues"
33
+ "Author Profile" = "https://github.com/Maxximuss-hub"
34
+
35
+ [tool.setuptools.packages.find]
36
+ where = ["."]
37
+ include = ["smartcache*"]
38
+ exclude = ["tests*", "examples*"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,5 @@
1
+ from .decorators.sync import cache
2
+ from .decorators.async_ import async_cache
3
+
4
+ __all__ = ["cache", "async_cache"]
5
+ __version__ = "0.2.0"
@@ -0,0 +1,35 @@
1
+ from abc import ABC, abstractmethod
2
+ from typing import Any
3
+
4
+
5
+ class _NotFound:
6
+ """Сентинел для отсутствующего значения."""
7
+ pass
8
+
9
+
10
+ class _Expired:
11
+ """Сентинел для истёкшего значения."""
12
+ pass
13
+
14
+
15
+ NOT_FOUND = _NotFound()
16
+ EXPIRED = _Expired()
17
+
18
+
19
+ class StorageBackend(ABC):
20
+ """Абстрактный интерфейс для любого хранилища."""
21
+
22
+ @abstractmethod
23
+ def get(self, key: str) -> Any:
24
+ """Возвращает значение или NOT_FOUND/EXPIRED."""
25
+ pass
26
+
27
+ @abstractmethod
28
+ def set(self, key: str, value: Any, ttl: int | None) -> None:
29
+ """Сохраняет значение."""
30
+ pass
31
+
32
+ @abstractmethod
33
+ def clear(self) -> None:
34
+ """Очищает хранилище."""
35
+ pass
@@ -0,0 +1,54 @@
1
+ import pickle
2
+ import time
3
+ from pathlib import Path
4
+ from typing import Any
5
+ from .base import StorageBackend, NOT_FOUND, EXPIRED
6
+
7
+
8
+ class DiskBackend(StorageBackend):
9
+ """Хранилище на диске (через pickle)."""
10
+
11
+ def __init__(self, cache_dir: str = ".smartcache"):
12
+ self._cache_dir = Path(cache_dir)
13
+ self._cache_dir.mkdir(exist_ok=True)
14
+ self._metadata_file = self._cache_dir / "metadata.pkl"
15
+
16
+ # Загружаем метаданные при инициализации
17
+ self._store = self._load_metadata()
18
+
19
+ def _load_metadata(self) -> dict:
20
+ """Загружает метаданные кэша с диска."""
21
+ if self._metadata_file.exists():
22
+ with open(self._metadata_file, "rb") as f:
23
+ return pickle.load(f)
24
+ return {}
25
+
26
+ def _save_metadata(self):
27
+ """Сохраняет метаданные на диск."""
28
+ with open(self._metadata_file, "wb") as f:
29
+ pickle.dump(self._store, f)
30
+
31
+ def get(self, key: str) -> Any:
32
+ if key not in self._store:
33
+ return NOT_FOUND
34
+
35
+ value, expire_at = self._store[key]
36
+
37
+ if expire_at is not None and time.monotonic() >= expire_at:
38
+ del self._store[key]
39
+ self._save_metadata()
40
+ return EXPIRED
41
+
42
+ return value
43
+
44
+ def set(self, key: str, value: Any, ttl: int | None) -> None:
45
+ expire_at = None
46
+ if ttl is not None:
47
+ expire_at = time.monotonic() + ttl
48
+
49
+ self._store[key] = (value, expire_at)
50
+ self._save_metadata()
51
+
52
+ def clear(self) -> None:
53
+ self._store.clear()
54
+ self._save_metadata()
@@ -0,0 +1,32 @@
1
+ import time
2
+ from typing import Any
3
+ from .base import StorageBackend, NOT_FOUND, EXPIRED
4
+
5
+
6
+ class MemoryBackend(StorageBackend):
7
+ """Хранилище в оперативной памяти."""
8
+
9
+ def __init__(self):
10
+ self._store: dict[str, tuple[Any, float | None]] = {}
11
+
12
+ def get(self, key: str) -> Any:
13
+ if key not in self._store:
14
+ return NOT_FOUND
15
+
16
+ value, expire_at = self._store[key]
17
+
18
+ if expire_at is not None and time.monotonic() >= expire_at:
19
+ del self._store[key]
20
+ return EXPIRED # ✅ Возвращаем EXPIRED вместо NOT_FOUND
21
+
22
+ return value
23
+
24
+ def set(self, key: str, value: Any, ttl: int | None) -> None:
25
+ expire_at = None
26
+ if ttl is not None:
27
+ expire_at = time.monotonic() + ttl
28
+
29
+ self._store[key] = (value, expire_at)
30
+
31
+ def clear(self) -> None:
32
+ self._store.clear()
@@ -0,0 +1,72 @@
1
+ from typing import Any
2
+ from collections import OrderedDict
3
+
4
+ from ..backends.base import StorageBackend, NOT_FOUND, EXPIRED
5
+ from ..utils.keymaker import generate_key
6
+ from .stats import CacheStats
7
+
8
+
9
+ class CacheEngine:
10
+ """Главное ядро кэша."""
11
+
12
+ def __init__(
13
+ self,
14
+ backend: StorageBackend,
15
+ max_size: int | None = None,
16
+ default_ttl: int | None = None,
17
+ ):
18
+ self._backend = backend
19
+ self._max_size = max_size
20
+ self._default_ttl = default_ttl
21
+ self._stats = CacheStats()
22
+ self._access_order: OrderedDict[str, None] = OrderedDict()
23
+
24
+ def get(self, func, args: tuple, kwargs: dict) -> Any:
25
+ """Пытается достать значение из кэша."""
26
+ key = generate_key((func.__name__, *args), kwargs)
27
+
28
+ if key in self._access_order:
29
+ self._access_order.move_to_end(key)
30
+
31
+ value = self._backend.get(key)
32
+
33
+ # ✅ Обрабатываем истечение TTL
34
+ if isinstance(value, EXPIRED.__class__):
35
+ self._stats.record_expired()
36
+ self._stats.record_miss()
37
+ return NOT_FOUND
38
+
39
+ if isinstance(value, NOT_FOUND.__class__):
40
+ self._stats.record_miss()
41
+ return NOT_FOUND
42
+
43
+ self._stats.record_hit()
44
+ return value
45
+
46
+ def set(self, func, args: tuple, kwargs: dict, value: Any, ttl: int | None = None) -> None:
47
+ """Сохраняет значение в кэш."""
48
+ key = generate_key((func.__name__, *args), kwargs)
49
+
50
+ if self._max_size is not None and len(self._access_order) >= self._max_size:
51
+ self._evict_oldest()
52
+
53
+ effective_ttl = ttl if ttl is not None else self._default_ttl
54
+ self._backend.set(key, value, effective_ttl)
55
+
56
+ self._access_order[key] = None
57
+
58
+ def _evict_oldest(self):
59
+ """Удаляет самую старую запись (LRU)."""
60
+ if self._access_order:
61
+ oldest_key, _ = self._access_order.popitem(last=False)
62
+ self._backend._store.pop(oldest_key, None)
63
+
64
+ def clear(self):
65
+ """Очищает весь кэш."""
66
+ self._backend.clear()
67
+ self._access_order.clear()
68
+ self._stats.reset()
69
+
70
+ @property
71
+ def stats(self) -> CacheStats:
72
+ return self._stats
@@ -0,0 +1,39 @@
1
+ class CacheStats:
2
+ """Сборщик статистики кэша."""
3
+
4
+ def __init__(self):
5
+ self.hits = 0
6
+ self.misses = 0
7
+ self.expired = 0
8
+
9
+ def record_hit(self):
10
+ self.hits += 1
11
+
12
+ def record_miss(self):
13
+ self.misses += 1
14
+
15
+ def record_expired(self):
16
+ self.expired += 1
17
+
18
+ @property
19
+ def hit_rate(self) -> float:
20
+ total = self.hits + self.misses
21
+ if total == 0:
22
+ return 0.0
23
+ return (self.hits / total) * 100
24
+
25
+ def __str__(self) -> str:
26
+ total = self.hits + self.misses
27
+ return (
28
+ f"📊 Cache Statistics\n"
29
+ f" Hits: {self.hits}\n"
30
+ f" Misses: {self.misses}\n"
31
+ f" Expired: {self.expired}\n"
32
+ f" Hit Rate: {self.hit_rate:.1f}%\n"
33
+ f" Total: {total}"
34
+ )
35
+
36
+ def reset(self):
37
+ self.hits = 0
38
+ self.misses = 0
39
+ self.expired = 0
@@ -0,0 +1,78 @@
1
+ import functools
2
+ from typing import Callable, Any
3
+ from ..core.engine import CacheEngine
4
+ from ..backends.memory import MemoryBackend
5
+ from ..backends.base import NOT_FOUND
6
+ from ..utils.logger import CacheLogger
7
+
8
+
9
+ def async_cache(
10
+ func: Callable | None = None,
11
+ *,
12
+ ttl: int | None = None,
13
+ max_size: int | None = None,
14
+ backend: Any = None,
15
+ log: bool = False,
16
+ ):
17
+ """
18
+ Асинхронный декоратор кэширования.
19
+ """
20
+
21
+ if func is not None and callable(func):
22
+ return _create_async_decorator()(func)
23
+ else:
24
+ return _create_async_decorator(
25
+ ttl=ttl,
26
+ max_size=max_size,
27
+ backend=backend,
28
+ log=log,
29
+ )
30
+
31
+
32
+ def _create_async_decorator(
33
+ ttl: int | None = None,
34
+ max_size: int | None = None,
35
+ backend: Any = None,
36
+ log: bool = False,
37
+ ):
38
+ """Создаёт асинхронный декоратор."""
39
+
40
+ if backend is None:
41
+ backend = MemoryBackend()
42
+
43
+ engine = CacheEngine(
44
+ backend=backend,
45
+ max_size=max_size,
46
+ default_ttl=ttl,
47
+ )
48
+
49
+ logger = CacheLogger() if log else None
50
+
51
+ def decorator(func: Callable) -> Callable:
52
+ @functools.wraps(func)
53
+ async def wrapper(*args, **kwargs):
54
+ if logger:
55
+ logger.log_attempt(func.__name__)
56
+
57
+ cached_value = engine.get(func, args, kwargs)
58
+
59
+ if cached_value is not NOT_FOUND:
60
+ if logger:
61
+ logger.log_hit(func.__name__)
62
+ return cached_value
63
+
64
+ if logger:
65
+ logger.log_miss(func.__name__)
66
+
67
+ result = await func(*args, **kwargs)
68
+
69
+ engine.set(func, args, kwargs, result, ttl=ttl)
70
+
71
+ return result
72
+
73
+ wrapper.cache_clear = engine.clear
74
+ wrapper.cache_stats = engine.stats
75
+
76
+ return wrapper
77
+
78
+ return decorator
@@ -0,0 +1,92 @@
1
+ import functools
2
+ from typing import Callable, Any
3
+ from ..core.engine import CacheEngine
4
+ from ..backends.memory import MemoryBackend
5
+ from ..backends.base import NOT_FOUND
6
+ from ..utils.logger import CacheLogger
7
+
8
+
9
+ def cache(
10
+ func: Callable | None = None,
11
+ *,
12
+ ttl: int | None = None,
13
+ max_size: int | None = None,
14
+ backend: Any = None,
15
+ log: bool = False,
16
+ ):
17
+ """
18
+ Умный декоратор кэширования.
19
+
20
+ Работает в двух режимах:
21
+ - @cache
22
+ - @cache(ttl=60, max_size=100)
23
+ """
24
+
25
+ # МАГИЯ: Определяем, как нас вызвали
26
+ if func is not None and callable(func):
27
+ # Вызов без скобок: @cache
28
+ return _create_decorator()(func)
29
+ else:
30
+ # Вызов со скобками: @cache(ttl=60)
31
+ return _create_decorator(
32
+ ttl=ttl,
33
+ max_size=max_size,
34
+ backend=backend,
35
+ log=log,
36
+ )
37
+
38
+
39
+ def _create_decorator(
40
+ ttl: int | None = None,
41
+ max_size: int | None = None,
42
+ backend: Any = None,
43
+ log: bool = False,
44
+ ):
45
+ """Создаёт декоратор с заданными параметрами."""
46
+
47
+ # Создаём движок кэша (или используем предоставленный бэкенд)
48
+ if backend is None:
49
+ backend = MemoryBackend()
50
+
51
+ engine = CacheEngine(
52
+ backend=backend,
53
+ max_size=max_size,
54
+ default_ttl=ttl,
55
+ )
56
+
57
+ logger = CacheLogger() if log else None
58
+
59
+ def decorator(func: Callable) -> Callable:
60
+ @functools.wraps(func)
61
+ def wrapper(*args, **kwargs):
62
+ # 🔥 Логгируем попытку
63
+ if logger:
64
+ logger.log_attempt(func.__name__)
65
+
66
+ # Пытаемся достать из кэша
67
+ cached_value = engine.get(func, args, kwargs)
68
+
69
+ if cached_value is not NOT_FOUND:
70
+ if logger:
71
+ logger.log_hit(func.__name__)
72
+ return cached_value
73
+
74
+ # Кэш не найден — вызываем функцию
75
+ if logger:
76
+ logger.log_miss(func.__name__)
77
+
78
+ result = func(*args, **kwargs)
79
+
80
+ # Сохраняем результат в кэш
81
+ engine.set(func, args, kwargs, result, ttl=ttl)
82
+
83
+ return result
84
+
85
+ # Добавляем методы для доступа к статистике и очистке
86
+ wrapper.cache_clear = engine.clear
87
+ wrapper.cache_stats = engine.stats
88
+ wrapper.cache_info = lambda: engine.stats # Для совместимости с lru_cache
89
+
90
+ return wrapper
91
+
92
+ return decorator
File without changes
@@ -0,0 +1,21 @@
1
+ import hashlib
2
+ import pickle
3
+
4
+ def generate_key(args: tuple, kwargs: dict) -> str:
5
+ """
6
+ Превращает ЛЮБЫЕ аргументы функции в уникальную строку-ключ.
7
+ """
8
+ # 1. Сортируем kwargs по ключам.
9
+ # Зачем? Чтобы вызовы func(a=1, b=2) и func(b=2, a=1)
10
+ # сгенерировали ОДИНАКОВЫЙ ключ.
11
+ sorted_kwargs = tuple(sorted(kwargs.items()))
12
+
13
+ # 2. Собираем всё в единый кортеж
14
+ data = (args, sorted_kwargs)
15
+
16
+ # 3. Сериализуем через pickle.
17
+ # Он умеет превращать в байты вообще всё (списки, словари, кастомные классы).
18
+ serialized_data = pickle.dumps(data)
19
+
20
+ # 4. Хэшируем байты в MD5, чтобы получить короткую и быструю строку
21
+ return hashlib.md5(serialized_data).hexdigest()
@@ -0,0 +1,14 @@
1
+ class CacheLogger:
2
+ """Простой логгер для кэша."""
3
+
4
+ def log_attempt(self, func_name: str):
5
+ pass # Можно добавить логирование попыток
6
+
7
+ def log_hit(self, func_name: str):
8
+ print(f"✅ CACHE HIT | {func_name}")
9
+
10
+ def log_miss(self, func_name: str):
11
+ print(f"❌ CACHE MISS | {func_name}")
12
+
13
+ def log_expired(self, func_name: str):
14
+ print(f"⏰ CACHE EXPIRED | {func_name}")
@@ -0,0 +1,49 @@
1
+ Metadata-Version: 2.4
2
+ Name: smartcache-maxximuss
3
+ Version: 1.0.0
4
+ Summary: High-performance Python caching library featuring TTL, LRU, async support, and disk/memory backends.
5
+ Author-email: Maxximuss <xxxmaxximussxxx@gmail.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/Maxximuss-hub/Smartcache
8
+ Project-URL: Bug Tracker, https://github.com/Maxximuss-hub/Smartcache/issues
9
+ Project-URL: Author Profile, https://github.com/Maxximuss-hub
10
+ Keywords: cache,caching,lru,ttl,async,performance,smartcache
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.8
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
20
+ Requires-Python: >=3.8
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ Dynamic: license-file
24
+
25
+ # SmartCache 🚀
26
+
27
+ Высокопроизводительная библиотека кэширования для Python.
28
+ **Создана для реальных задач: экономия ресурсов, ускорение API, работа с асинхронными запросами.**
29
+
30
+ [![PyPI](https://img.shields.io/pypi/v/smartcache)](https://pypi.org/project/smartcache/)
31
+ [![Python](https://img.shields.io/badge/Python-3.8%2B-blue)]()
32
+ [![License](https://img.shields.io/badge/License-MIT-green)](LICENSE)
33
+
34
+ ## 💡 Зачем это нужно?
35
+
36
+ Представь, что твой API-запрос стоит $0.01.
37
+ 1000 пользователей одновременно запрашивают погоду → **$10 вместо $0.01**.
38
+ С `SmartCache` ты экономишь **99.9%** запросов!
39
+
40
+ ## 🚀 Установка
41
+
42
+ ```bash
43
+ # Установка из PyPI (когда будет опубликовано)
44
+ pip install smartcache
45
+
46
+ # Или локальная установка (в разработке)
47
+ git clone https://github.com/yourusername/smartcache.git
48
+ cd smartcache
49
+ pip install -e .
@@ -0,0 +1,22 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ smartcache/__init__.py
5
+ smartcache/exceptions.py
6
+ smartcache/backends/base.py
7
+ smartcache/backends/disk.py
8
+ smartcache/backends/memory.py
9
+ smartcache/core/engine.py
10
+ smartcache/core/stats.py
11
+ smartcache/decorators/async_.py
12
+ smartcache/decorators/sync.py
13
+ smartcache/utils/keymaker.py
14
+ smartcache/utils/logger.py
15
+ smartcache_maxximuss.egg-info/PKG-INFO
16
+ smartcache_maxximuss.egg-info/SOURCES.txt
17
+ smartcache_maxximuss.egg-info/dependency_links.txt
18
+ smartcache_maxximuss.egg-info/top_level.txt
19
+ tests/test_async_disk.py
20
+ tests/test_final.py
21
+ tests/test_step1.py
22
+ tests/test_step2.py
@@ -0,0 +1,57 @@
1
+ import asyncio
2
+ from smartcache import async_cache
3
+ from smartcache.backends.disk import DiskBackend
4
+
5
+
6
+ # ТЕСТ 1: Async с TTL
7
+ @async_cache(ttl=3, log=True)
8
+ async def fetch_data(url: str):
9
+ """Имитация асинхронного запроса."""
10
+ print(f" 🌐 Запрос к {url}...")
11
+ await asyncio.sleep(1)
12
+ return f"Data from {url}"
13
+
14
+
15
+ # ТЕСТ 2: Disk Backend (сохранение между запусками)
16
+ disk_backend = DiskBackend("./my_cache")
17
+
18
+ @async_cache(backend=disk_backend, log=True)
19
+ async def heavy_computation(x: int):
20
+ """Тяжелое вычисление с сохранением на диск."""
21
+ print(f" Вычисляю {x}^2...")
22
+ await asyncio.sleep(1)
23
+ return x ** 2
24
+
25
+
26
+ async def main():
27
+ print("=" * 50)
28
+ print("ТЕСТ 1: Async Cache")
29
+ print("=" * 50)
30
+
31
+ result1 = await fetch_data("https://api.example.com")
32
+ print(f"Результат: {result1}")
33
+
34
+ result2 = await fetch_data("https://api.example.com")
35
+ print(f"Из кэша: {result2}")
36
+
37
+ print("\nЖдём 4 секунды...")
38
+ await asyncio.sleep(4)
39
+
40
+ result3 = await fetch_data("https://api.example.com")
41
+ print(f"После TTL: {result3}")
42
+
43
+ print("\n" + "=" * 50)
44
+ print("ТЕСТ 2: Disk Backend")
45
+ print("=" * 50)
46
+
47
+ result4 = await heavy_computation(42)
48
+ print(f"Результат: {result4}")
49
+
50
+ result5 = await heavy_computation(42)
51
+ print(f"Из кэша: {result5}")
52
+
53
+ print("\n✅ Кэш сохранён на диск! Перезапусти скрипт — он загрузится.")
54
+
55
+
56
+ if __name__ == "__main__":
57
+ asyncio.run(main())
@@ -0,0 +1,53 @@
1
+ import time
2
+ from smartcache import cache
3
+
4
+ # 🔥 ТЕСТ 1: Декоратор без скобок
5
+ @cache
6
+ def fibonacci_slow(n):
7
+ """Медленная функция Фибоначчи."""
8
+ if n < 2:
9
+ return n
10
+ return fibonacci_slow(n-1) + fibonacci_slow(n-2)
11
+
12
+ # 🔥 ТЕСТ 2: Декоратор со скобками
13
+ @cache(ttl=3, max_size=2, log=True)
14
+ def expensive_calculation(x, y):
15
+ """Дорогая функция с TTL."""
16
+ print(f" 🔄 Вычисляю {x} + {y}...")
17
+ time.sleep(1)
18
+ return x + y
19
+
20
+ # ТЕСТ 3: Функция с нехэшируемыми аргументами
21
+ @cache
22
+ def process_data(data_dict):
23
+ """Функция со словарём (нехэшируемый аргумент)."""
24
+ return sum(data_dict.values())
25
+
26
+
27
+ if __name__ == "__main__":
28
+ print("=" * 50)
29
+ print("ТЕСТ 1: Fibonacci (без скобок)")
30
+ print("=" * 50)
31
+ print(f"fib(10) = {fibonacci_slow(10)}")
32
+ print(f"fib(10) из кэша = {fibonacci_slow(10)}")
33
+ print(fibonacci_slow.cache_stats)
34
+
35
+ print("\n" + "=" * 50)
36
+ print("ТЕСТ 2: Expensive Calculation (с TTL и логами)")
37
+ print("=" * 50)
38
+ print(f"Результат: {expensive_calculation(5, 10)}")
39
+ print(f"Результат из кэша: {expensive_calculation(5, 10)}")
40
+
41
+ print("\nЖдём 4 секунды (TTL=3)...")
42
+ time.sleep(4)
43
+
44
+ print(f"Результат после TTL: {expensive_calculation(5, 10)}")
45
+ print(expensive_calculation.cache_stats)
46
+
47
+ print("\n" + "=" * 50)
48
+ print("ТЕСТ 3: Нехэшируемые аргументы")
49
+ print("=" * 50)
50
+ data = {"a": 1, "b": 2, "c": 3}
51
+ print(f"process_data(data) = {process_data(data)}")
52
+ print(f"process_data(data) из кэша = {process_data(data)}")
53
+ print(process_data.cache_stats)
@@ -0,0 +1,25 @@
1
+ import time
2
+ from smartcache.utils.keymaker import generate_key
3
+ from smartcache.backends.memory import MemoryBackend, NOT_FOUND
4
+
5
+ # 1. Тестируем генератор ключей
6
+ key1 = generate_key((1, 2), {'a': 5, 'b': [1, 2]})
7
+ key2 = generate_key((1, 2), {'b': [1, 2], 'a': 5}) # Порядок kwargs другой!
8
+ print(f"Ключи совпадают? {key1 == key2}") # Должно быть True
9
+
10
+ # 2. Тестируем бэкенд
11
+ cache = MemoryBackend()
12
+
13
+ # Сохраняем на 2 секунды
14
+ cache.set("test_key", "Hello World", ttl=2)
15
+
16
+ # Достаем сразу
17
+ val = cache.get("test_key")
18
+ print(f"Сразу: {val}") # Hello World
19
+
20
+ # Ждем 3 секунды
21
+ time.sleep(3)
22
+
23
+ # Достаем после истечения TTL
24
+ val_expired = cache.get("test_key")
25
+ print(f"Через 3 сек: {val_expired is NOT_FOUND}") # Должно быть True
@@ -0,0 +1,37 @@
1
+ import time
2
+ from smartcache.backends.memory import MemoryBackend
3
+ from smartcache.core.engine import CacheEngine
4
+ from smartcache.backends.base import NOT_FOUND
5
+
6
+ # Создаем движок с лимитом 3 записи и TTL по умолчанию 2 сек
7
+ backend = MemoryBackend()
8
+ engine = CacheEngine(backend, max_size=3, default_ttl=2)
9
+
10
+ def dummy_func():
11
+ pass
12
+
13
+ # 1. Сохраняем 3 значения
14
+ engine.set(dummy_func, (1,), {}, "A")
15
+ engine.set(dummy_func, (2,), {}, "B")
16
+ engine.set(dummy_func, (3,), {}, "C")
17
+ print(engine.stats) # Все misses
18
+
19
+ # 2. Достаем "A" — оно становится "свежим"
20
+ val = engine.get(dummy_func, (1,), {})
21
+ print(f"Достали A: {val}") # A
22
+ print(engine.stats) # 1 hit
23
+
24
+ # 3. Добавляем 4-е значение — должно вытисниться самое старое ("B")
25
+ engine.set(dummy_func, (4,), {}, "D")
26
+
27
+ # 4. Пытаемся достать "B" — его уже нет!
28
+ val_b = engine.get(dummy_func, (2,), {})
29
+ print(f"B вытеснен? {val_b is NOT_FOUND}") # True
30
+
31
+ # 5. Ждем истечения TTL
32
+ time.sleep(3)
33
+ val_a = engine.get(dummy_func, (1,), {})
34
+ print(f"A истек? {val_a is NOT_FOUND}") # True
35
+
36
+ print("\nИтоговая статистика:")
37
+ print(engine.stats)