Skip to content

Commit

Permalink
Renamed local_cache to persist_cache as local_cache is not avai…
Browse files Browse the repository at this point in the history
…lable on PyPi.
  • Loading branch information
umarbutler committed Mar 12, 2024
1 parent bb1bc04 commit 8ce13b4
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 34 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ jobs:
python -m pip install pytest
python -m pip install pytest-cov
- name: Install local-cache
- name: Install persist-cache
run: |
python -m pip install .
- name: Test with pytest
run: |
pytest --cov=local_cache --cov-report=xml
pytest --cov=persist_cache --cov-report=xml
- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: umarbutler/local-cache
slug: umarbutler/persist-cache
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
## Changelog 🔄
All notable changes to `local-cache` will be documented here. This project adheres to [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
All notable changes to `persist-cache` will be documented here. This project adheres to [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.0] - 2024-03-12
### Added
- Added the `cache()` decorator, which locally and persistently caches functions.

[0.1.0]: https://github.com/umarbutler/local-cache/releases/tag/v0.1.0
[0.1.0]: https://github.com/umarbutler/persist-cache/releases/tag/v0.1.0
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
# local-cache
<a href="https://pypi.org/project/local-cache/" alt="PyPI Version"><img src="https://img.shields.io/pypi/v/local-cache"></a> <a href="https://github.com/umarbutler/local-cache/actions/workflows/ci.yml" alt="Build Status"><img src="https://img.shields.io/github/actions/workflow/status/umarbutler/local-cache/ci.yml?branch=main"></a> <a href="https://app.codecov.io/gh/umarbutler/local-cache" alt="Code Coverage"><img src="https://img.shields.io/codecov/c/github/umarbutler/local-cache"></a> <!-- <a href="https://pypistats.org/packages/local-cache" alt="Downloads"><img src="https://img.shields.io/pypi/dm/local-cache"></a> -->
# persist-cache
<a href="https://pypi.org/project/persist-cache/" alt="PyPI Version"><img src="https://img.shields.io/pypi/v/persist-cache"></a> <a href="https://github.com/umarbutler/persist-cache/actions/workflows/ci.yml" alt="Build Status"><img src="https://img.shields.io/github/actions/workflow/status/umarbutler/persist-cache/ci.yml?branch=main"></a> <a href="https://app.codecov.io/gh/umarbutler/persist-cache" alt="Code Coverage"><img src="https://img.shields.io/codecov/c/github/umarbutler/persist-cache"></a> <!-- <a href="https://pypistats.org/packages/persist-cache" alt="Downloads"><img src="https://img.shields.io/pypi/dm/persist-cache"></a> -->

`local-cache` is an *easy-to-use* Python library for lightning-fast persistent function caching. It is capable of caching both synchronous and asynchronous functions as well as methods, and is also process-safe and thread-safe.
`persist-cache` is an *easy-to-use* Python library for lightning-fast persistent function caching. It is capable of caching both synchronous and asynchronous functions as well as methods, and is also process-safe and thread-safe.

## Features 🎯
- **⚡ Lightning-fast**: `local-cache` can cache a function call in 400 nanoseconds and return it back in a single millisecond.
- **⚡ Lightning-fast**: `persist-cache` can cache a function call in 400 nanoseconds and return it back in a single millisecond.
- **💽 Persistent**: cached returns persist across sessions and are stored locally.
- **⌛ Stale-free**: cached returns may be given shelf lives, after which they will be automatically flushed out.
- **🦺 Process- and thread-safe**: interprocess file locks prevent processes and threads from writing over each other.
- **⏱️ Async-compatible**: asynchronous functions can be cached with the same decorator as synchronous ones.
- **👨‍🏫 Class-compatible**: methods can be cached with the same decorator as functions (although the `self` argument will always be ignored).

## Installation 📦
`local-cache` may be installed with `pip`:
`persist-cache` may be installed with `pip`:
```bash
pip install local-cache
pip install persist-cache
```

## Usage 👩‍💻
The code snippet below demonstrates how both synchronous and asynchronous functions as well as methods can be cached with `local-cache`:
The code snippet below demonstrates how both synchronous and asynchronous functions as well as methods can be cached with `persist-cache`:
```python
from local_cache import cache
from persist_cache import cache

@cache
def my_function(): ...
Expand Down Expand Up @@ -68,7 +68,7 @@ The function to be cached must accept and return [dillable](https://dill.readthe

`name` represents the name of the cache (or, if `cache()` is being called as an argument-less decorator (ie, as `@cache` instead of `@cache(...)`), the function to be cached). It defaults to the hash of the qualified name of the function. If `dir` is set, `name` will be ignored.

`dir` represents the directory in which the cache should be stored. It defaults to a subdirectory bearing the name of the cache in a parent folder called '.local_cache' in the current working directory.
`dir` represents the directory in which the cache should be stored. It defaults to a subdirectory bearing the name of the cache in a parent folder called '.persist_cache' in the current working directory.

`expiry` represents how long, in seconds or as a `timedelta`, function calls should persist in the cache. It defaults to `None`.

Expand All @@ -81,4 +81,4 @@ After being wrapped, the cached function will have the following methods attache
- `delete_cache() -> None`: Deletes the cache.

## License 📜
This library is licensed under the [MIT License](https://github.com/umarbutler/local-cache/blob/main/LICENCE).
This library is licensed under the [MIT License](https://github.com/umarbutler/persist-cache/blob/main/LICENCE).
10 changes: 5 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "local-cache"
name = "persist-cache"
version = "0.1.0"
authors = [
{name="Umar Butler", email="[email protected]"},
Expand Down Expand Up @@ -57,7 +57,7 @@ dependencies = [
]

[project.urls]
Homepage = "https://github.com/umarbutler/local-cache"
Documentation = "https://github.com/umarbutler/local-cache/blob/main/README.md"
Issues = "https://github.com/umarbutler/local-cache/issues"
Source = "https://github.com/umarbutler/local-cache"
Homepage = "https://github.com/umarbutler/persist-cache"
Documentation = "https://github.com/umarbutler/persist-cache/blob/main/README.md"
Issues = "https://github.com/umarbutler/persist-cache/issues"
Source = "https://github.com/umarbutler/persist-cache"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""An easy-to-use Python library for lightning-fast persistent function caching."""

from .local_cache import cache
from .persist_cache import cache
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def cache(
Arguments:
name (`str | Callable`, optional): The name of the cache (or, if `cache()` is being called as an argument-less decorator (ie, as `@cache` instead of `@cache(...)`), the function to be cached). Defaults to the hash of the qualified name of the function. If `dir` is set, this argument will be ignored.
dir (`str`, optional): The directory in which the cache should be stored. Defaults to a subdirectory bearing the name of the cache in a parent folder called '.local_cache' in the current working directory.
dir (`str`, optional): The directory in which the cache should be stored. Defaults to a subdirectory bearing the name of the cache in a parent folder called '.persist_cache' in the current working directory.
expiry (`int | float | timedelta`, optional): How long, in seconds or as a `timedelta`, function calls should persist in the cache. Defaults to `None`.
Expand All @@ -36,12 +36,12 @@ def cache(
def decorator(func: Callable) -> Callable:
nonlocal name, dir, expiry

# If the cache directory has not been set, and the name of the cache has, set it to a subdirectory by that name in a directory called '.local_cache' in the current working directory, or, if the name of the cache has not been set, set the name of that subdirectory to the hash of the qualified name of the function.
# If the cache directory has not been set, and the name of the cache has, set it to a subdirectory by that name in a directory called '.persist_cache' in the current working directory, or, if the name of the cache has not been set, set the name of that subdirectory to the hash of the qualified name of the function.
if dir is None:
if name is not None:
dir = f'.local_cache/{name}'
dir = f'.persist_cache/{name}'
else:
dir = f'.local_cache/{caching.hash(func.__qualname__)}'
dir = f'.persist_cache/{caching.hash(func.__qualname__)}'

# Create the cache directory and any other necessary directories if it does not exist.
if not os.path.exists(dir):
Expand Down
File renamed without changes.
File renamed without changes.
18 changes: 9 additions & 9 deletions tests/test_local_cache.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Test local-cache."""
"""Test persist-cache."""
import os
import random
import shutil
import time
from typing import Callable, Union

import local_cache
import persist_cache


def _time_consuming_function(
Expand Down Expand Up @@ -96,29 +96,29 @@ def _test_cached_function(cached_function: Callable, dir: str = None, expiry: in
cached_function.delete_cache()
assert not os.path.exists(dir)

def test_local_cache() -> None:
"""Test `local_cache.local_cache()`."""
def test_persist_cache() -> None:
"""Test `persist_cache.persist_cache()`."""

# Cache and test the time-consuming function without arguments.
cached_function = local_cache.cache(_time_consuming_function)
cached_function = persist_cache.cache(_time_consuming_function)
print('Testing the caching of the time-consuming function without arguments.')
_test_cached_function(cached_function)

# Cache and test the time-consuming function with arguments.
cached_function = local_cache.cache()(_time_consuming_function)
cached_function = persist_cache.cache()(_time_consuming_function)
print('Testing the caching of the time-consuming function with arguments.')
_test_cached_function(cached_function)

# Cache and test the time-consuming function with a time-to-live.
cached_function = local_cache.cache(expiry=1)(_time_consuming_function)
cached_function = persist_cache.cache(expiry=1)(_time_consuming_function)
print('Testing the caching of the time-consuming function with a time-to-live.')
_test_cached_function(cached_function, expiry=1)

# Cache and test the time-consuming function with a custom directory.
cached_function = local_cache.cache(dir='.custom_cache')(_time_consuming_function)
cached_function = persist_cache.cache(dir='.custom_cache')(_time_consuming_function)
print('Testing the caching of the time-consuming function with a custom directory.')
_test_cached_function(cached_function, dir='.custom_cache')

# Remove cache directories.
for dir in {'.local_cache', '.custom_cache'}:
for dir in {'.persist_cache', '.custom_cache'}:
shutil.rmtree(dir, ignore_errors=True)

0 comments on commit 8ce13b4

Please sign in to comment.