https://github.com/othercodes/py-cache
Small cache implementation for python.
https://github.com/othercodes/py-cache
Last synced: over 1 year ago
JSON representation
Small cache implementation for python.
- Host: GitHub
- URL: https://github.com/othercodes/py-cache
- Owner: othercodes
- License: apache-2.0
- Created: 2022-02-24T19:31:34.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-01-09T11:46:38.000Z (over 3 years ago)
- Last Synced: 2025-01-24T08:12:31.434Z (over 1 year ago)
- Language: Python
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Py-Cache
[](https://github.com/othercodes/py-cache/actions/workflows/test.yml)
[](https://codecov.io/gh/othercodes/py-cache)
Small cache implementation for python.
## Installation
```bash
poetry add git+https://github.com/othercodes/py-cache.git
```
## Usage
Just initialize the class you want and use the public methods:
```python
from py_cache.contracts import Cache
from py_cache.sqlite import SQLiteCache
def some_process_that_requires_cache(cache: Cache):
# retrieve the data from cache, ir the key is not cached yet and the default
# value is a callable the cache will use it to compute and cache the value
user = cache.get('user-id', lambda: db_get_user('user-id'))
print(user)
# inject the desired cache adapter.
cache = SQLiteCache('/tmp', 900)
some_process_that_requires_cache(cache)
```
Checking the if the key exists in the cache.
```python
cache.has('user-id')
```
Getting a value from the cache.
```python
# will return None if the value not exists in the cache.
cache.get('user-id')
# if the key is not present, the default value will be used.
cache.get('user-id', {'id': 'user-id', 'email': 'vincent.vega@mail.com'})
# if the default value is a callable the cache will use the callable to
# compute and cache the request value.
cache.get('user-id', lambda: db_get_user('user-id'))
# you can also provide custom ttl (time to live) for a computed value by
# returning a tuple of computed value and the ttl integer (Tuple[Any, int]).
cache.get('user-id', lambda: (db_get_user('user-id'), 3600))
```
Storing value by key in cache.
```python
# store the given value by key.
cache.put('user-id', {'id': 'user-id', 'email': 'vincent.vega@mail.com'})
# store the given value with custom ttl (time to live).
cache.put('user-id', {'id': 'user-id', 'email': 'vincent.vega@mail.com'}, 3600)
```
Delete a value from cache by key.
```python
cache.delete('user-id')
```
Flush the complete cache.
```python
cache.flush()
```
## Adapters
| Adapter | Description |
|------------------|--------------------------------------------|
| FileSystemCache | Uses simple json files to store the cache. |
| SQLiteCache | SQLite will be used as cache. |