Skip to main content

Python Implements Caching

Project description

PyPI Travis build on master Travis build on develop Codecov coverage Chat on Gitter License

Python Implements Caching

特点

✓ 支持的缓存策略 :random, LRU

✓ 使用装饰器的形式来使用缓存

✓ 支持 Hashable keys (dictionaries, lists, sets)

✓ 支持超时缓存处理

✓ 支持缓存数据统计

安装

pip install mycache

使用

cache 装饰器

from mycache import cache


@cache()
def example():
    print("你好,这里是mycache测试案例")


example()  # 你好,这里是mycache测试案例
example()  # 没有任何输出
import time

from mycache import cache


@cache()
def long_computation(x):
    print("Performing long computation...")
    time.sleep(1)
    return x + 1


long_computation(5)  # Sleeps for 1 second and returns 6
long_computation(5)  # Immediately returns 6

long_computation(6)  # Sleeps for 1 second and returns 7
long_computation(6)  # Immediately returns 7
long_computation(6)  # And again

缓存类

import time
from datetime import timedelta

from mycache import Cache

cache = Cache()
cache.save("a", 1)
cache.save("b", 2)
cache.save("c", 3, expire_in=timedelta(seconds=10))

cache.has("c")  # returns True
cache.get("a")  # returns 1

time.sleep(10)
cache.has("c")  # False
cache.get("c")  # raises KeyError

设置缓存类型

from mycache import Cache
from mycache.policies import LRU

"""
设置缓存处理方式和缓存个数
"""

cache = Cache(max_items=2, replacement_policy=LRU())
cache.save("a", 1)
cache.save("b", 2)
cache.save("c", 3)

cache.has("a")  # returns False
cache.has("b")  # returns True

cache.save("d", 4)

cache.has("b")  # returns False

Disable deepcopy for keys

from mycache import cache

"""
缓存类和缓存装饰器接受' copy_keys '参数。
如果你能保证即使键是可变的也不会改变,
你可以把它设置为“True”来加快速度。
"""


@cache(copy_keys=False)
def faster_caching(x):
    return x


faster_caching({1, 2, 3})  # returns {1, 2, 3}

使用相关工具

  1. make lint: pylint and pycodestyle
  2. make typecheck: mypy
  3. make test: pytest
  4. make coverage: pytest with pytest-cov
  5. make quality: radon
  6. make build: setup.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

mycache-0.0.2-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

Details for the file mycache-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: mycache-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.7.1

File hashes

Hashes for mycache-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7f414708605f5e6b671da78e3d6e9e58dad6e0a5be09899444eae1ae6cdc8366
MD5 d3b031d040c487aeaf7f04264283f52f
BLAKE2b-256 a482effc7fb5b3d14a86624ce1c33935bd65e07f44934d50d92c4d6e22e05b8b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page