Skip to main content

Logo

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

A bunch of caches.

Features

✓ Ease of use cache decorator

✓ Support for non-Hashable keys (dictionaries, lists, sets)

✓ Different cache replacement policies (random, LRU)

✓ Time-based item expiration

□ Cache hit/miss statistics

□ Rich configuration, sane defaults

□ Optional persistency

Installation

Recommended way is to use poetry:

poetry install pycaches

But you also can install library with pip:

pip install pycaches

Usage

cache decorator

from pycaches import cache


@cache()
def example():
    print("Hi, I will be called once!")


example()  # Prints "Hi, I will be called once!"
example()  # Is not called
import time

from pycaches 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

Cache class

import time
from datetime import timedelta

from pycaches 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

Different cache replacement policies

from pycaches import Cache
from pycaches.policies import LRU

"""
LRU stands for Least Recently Used.
So LRU policy removes Least Recently Used item from cache
if it's size exceed max_items.
"""


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 pycaches import cache

"""
Cache class and cache decorator accepts `copy_keys` argument.
If you can garantee that keys will not change even if they are mutable,
you may set it to `True` to speed things up.
"""


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


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

Contribution

Just clone repository, make your changes and create a pull request.

Do not forget to make sure code quality is high: run linters, typecheckers, check code coverage, etc. You can do it all with make:

  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

And just make or make all to run all these targets.

Metadata

Release files for pycaches 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pycaches 0.0.7
File Size Uploaded
pycaches-0.0.7.tar.gz 6.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pycaches 0.0.7
File Interpreter ABI Platform
pycaches-0.0.7-py3-none-any.whl Python 3 none any Details

Total release size: 13.6 kB

Release files / pycaches-0.0.7.tar.gz

Download URL pycaches-0.0.7.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
a1f0bd058496d82b448785b2bac450d86fda7af76d53fe75c962a409bb919214
BLAKE2b-256 checksum
How to use checksums
2596f42e0ddd69529a83b5ca6340c78f2bd696fe40a5a4299286ba0344e4a48a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.3 CPython/3.8.0 Linux/4.15.0-1077-gcp

Release files / pycaches-0.0.7-py3-none-any.whl

Download URL pycaches-0.0.7-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
09840511c953c62dc0bbbc4481b1bf8404bed3d027bb730074cb4bbaa47b44b5
BLAKE2b-256 checksum
How to use checksums
ee08fbb0be1b6ea2302d24fd3fa685bcec4cecc5835fadd4307fbd9998e4297f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.3 CPython/3.8.0 Linux/4.15.0-1077-gcp

Release history Release notifications | RSS feed

This release

0.0.7 This release

2 release files

0.0.6

2 release files

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page