• Stars
    star
    415
  • Rank 104,301 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 7 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A caching library for Python

cacheout

version build coveralls license

A caching library for Python.

Links

Features

  • In-memory caching using dictionary backend
  • Cache manager for easily accessing multiple cache objects
  • Reconfigurable cache settings for runtime setup when using module-level cache objects
  • Maximum cache size enforcement
  • Default cache TTL (time-to-live) as well as custom TTLs per cache entry
  • Bulk set, get, and delete operations
  • Bulk get and delete operations filtered by string, regex, or function
  • Memoization decorators
  • Thread safe
  • Multiple cache implementations:
    • FIFO (First In, First Out)
    • LIFO (Last In, First Out)
    • LRU (Least Recently Used)
    • MRU (Most Recently Used)
    • LFU (Least Frequently Used)
    • RR (Random Replacement)

Roadmap

  • Layered caching (multi-level caching)
  • Cache event listener support (e.g. on-get, on-set, on-delete)
  • Cache statistics (e.g. cache hits/misses, cache frequency, etc)

Requirements

  • Python >= 3.7

Quickstart

Install using pip:

pip install cacheout

Let's start with some basic caching by creating a cache object:

from cacheout import Cache

cache = Cache()

By default the cache object will have a maximum size of 256, default TTL (time-to-live) expiration turned off, TTL timer that uses time.time (meaning TTL is in seconds), and the default for missing keys as None. These values can be set with:

cache = Cache(maxsize=256, ttl=0, timer=time.time, default=None)  # defaults

Set a cache key using cache.set():

cache.set(1, 'foobar')

Get the value of a cache key with cache.get():

assert cache.get(1) == 'foobar'

Get a default value when cache key isn't set:

assert cache.get(2) is None
assert cache.get(2, default=False) is False
assert 2 not in cache

Provide cache values using a default callable:

assert 2 not in cache
assert cache.get(2, default=lambda key: key) == 2
assert cache.get(2) == 2
assert 2 in cache

Provide a global default:

cache2 = Cache(default=True)
assert cache2.get('missing') is True
assert 'missing' not in cache2

cache3 = Cache(default=lambda key: key)
assert cache3.get('missing') == 'missing'
assert 'missing' in cache3

Set the TTL (time-to-live) expiration per entry (default TTL units are in seconds when Cache.timer is set to the default time.time; otherwise, the units are determined by the custom timer function):

cache.set(3, {'data': {}}, ttl=1)
assert cache.get(3) == {'data': {}}
time.sleep(1)
assert cache.get(3) is None

Memoize a function where cache keys are generated from the called function parameters:

@cache.memoize()
def func(a, b):
    pass

Provide a TTL for the memoized function and incorporate argument types into generated cache keys:

@cache.memoize(ttl=5, typed=True)
def func(a, b):
    pass

# func(1, 2) has different cache key than func(1.0, 2.0), whereas,
# with "typed=False" (the default), they would have the same key

Access the original memoized function:

@cache.memoize()
def func(a, b):
    pass

func.uncached(1, 2)

Get a copy of the entire cache with cache.copy():

assert cache.copy() == {1: 'foobar', 2: ('foo', 'bar', 'baz')}

Delete a cache key with cache.delete():

cache.delete(1)
assert cache.get(1) is None

Clear the entire cache with cache.clear():

cache.clear()
assert len(cache) == 0

Perform bulk operations with cache.set_many(), cache.get_many(), and cache.delete_many():

cache.set_many({'a': 1, 'b': 2, 'c': 3})
assert cache.get_many(['a', 'b', 'c']) == {'a': 1, 'b': 2, 'c': 3}
cache.delete_many(['a', 'b', 'c'])
assert cache.count() == 0

Use complex filtering in cache.get_many() and cache.delete_many():

import re
cache.set_many({'a_1': 1, 'a_2': 2, '123': 3, 'b': 4})

cache.get_many('a_*') == {'a_1': 1, 'a_2': 2}
cache.get_many(re.compile(r'\d')) == {'123': 3}
cache.get_many(lambda key: '2' in key) == {'a_2': 2, '123': 3}

cache.delete_many('a_*')
assert dict(cache.items()) == {'123': 3, 'b': 4}

Reconfigure the cache object after creation with cache.configure():

cache.configure(maxsize=1000, ttl=5 * 60)

Get keys, values, and items from the cache with cache.keys(), cache.values(), and cache.items():

cache.set_many({'a': 1, 'b': 2, 'c': 3})
assert list(cache.keys()) == ['a', 'b', 'c']
assert list(cache.values()) == [1, 2, 3]
assert list(cache.items()) == [('a', 1), ('b', 2), ('c', 3)]

Iterate over cache keys:

for key in cache:
    print(key, cache.get(key))
    # 'a' 1
    # 'b' 2
    # 'c' 3

Check if key exists with cache.has() and key in cache:

assert cache.has('a')
assert 'a' in cache

Manage multiple caches using CacheManager:

from cacheout import CacheManager

cacheman = CacheManager({'a': {'maxsize': 100},
                         'b': {'maxsize': 200, 'ttl': 900},
                         'c': {})

cacheman['a'].set('key1', 'value1')
value = cacheman['a'].get('key')

cacheman['b'].set('key2', 'value2')
assert cacheman['b'].maxsize == 200
assert cacheman['b'].ttl == 900

cacheman['c'].set('key3', 'value3')

cacheman.clear_all()
for name, cache in cacheman:
    assert name in cacheman
    assert len(cache) == 0

For more details, see the full documentation at https://cacheout.readthedocs.io.

More Repositories

1

pydash

The kitchen sink of Python utility libraries for doing "stuff" in a functional way. Based on the Lo-Dash Javascript library.
Python
1,291
star
2

fnc

Functional programming in Python with generators and other utilities.
Python
247
star
3

hashfs

A content-addressable file management system for Python.
Python
212
star
4

sqlservice

The missing SQLAlchemy ORM interface.
Python
178
star
5

pushjack

Push notifications for APNS (iOS) and GCM (Android).
Python
129
star
6

omdb.py

Python wrapper around OMDb API (Open Movie Database): http://omdbapi.com
Python
98
star
7

alchy

The declarative companion to SQLAlchemy
Python
75
star
8

shelmet

A shell power-up for working with the file system and running subprocess commands
Python
74
star
9

flask-pushjack

Flask extension for push notifications on APNS (iOS) and GCM (Android)
Python
70
star
10

verify

A painless assertion and validation library for Python.
Python
66
star
11

zulu

A drop-in replacement for native Python datetimes that embraces UTC.
Python
60
star
12

yummly.py

Python library for Yummly API: https://developer.yummly.com
Python
28
star
13

flask-logconfig

Flask extension for configuring Python logging module
Python
23
star
14

flask-alchy

Flask extension for alchy, the SQLAlchemy enhancement library
Python
17
star
15

logconfig

Simple helper moudle for configuring Python logging
Python
10
star
16

flask-hashfs

Flask extension for HashFS, a content-addressable file management system
Python
7
star
17

pixif

Python script for moving/copying photos from a directory and saving them based on EXIF tag data.
Python
6
star
18

ladder

HTTP client wrapper with URL generation via object notation and argument passing
Python
6
star
19

schemable

Schema validation and parsing library
Python
3
star
20

blog

Source code for my blog
Python
3
star
21

carafe

Flask application factory with extensions geared towards JSON APIs
Python
2
star