{"id":21540503,"url":"https://github.com/othercodes/py-cache","last_synced_at":"2025-03-17T21:45:57.802Z","repository":{"id":40299197,"uuid":"463276767","full_name":"othercodes/py-cache","owner":"othercodes","description":"Small cache implementation for python.","archived":false,"fork":false,"pushed_at":"2023-01-09T11:46:38.000Z","size":24,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-24T08:12:31.434Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/othercodes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null}},"created_at":"2022-02-24T19:31:34.000Z","updated_at":"2022-03-16T08:55:15.000Z","dependencies_parsed_at":"2023-02-08T10:32:02.421Z","dependency_job_id":null,"html_url":"https://github.com/othercodes/py-cache","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/othercodes%2Fpy-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/othercodes%2Fpy-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/othercodes%2Fpy-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/othercodes%2Fpy-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/othercodes","download_url":"https://codeload.github.com/othercodes/py-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244117101,"owners_count":20400739,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-24T04:19:11.485Z","updated_at":"2025-03-17T21:45:57.784Z","avatar_url":"https://github.com/othercodes.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Py-Cache\n\n[![Test](https://github.com/othercodes/py-cache/actions/workflows/test.yml/badge.svg)](https://github.com/othercodes/py-cache/actions/workflows/test.yml)\n[![codecov](https://codecov.io/gh/othercodes/py-cache/branch/master/graph/badge.svg?token=MSNTHXcCVC)](https://codecov.io/gh/othercodes/py-cache)\n\nSmall cache implementation for python.\n\n## Installation\n\n```bash\npoetry add git+https://github.com/othercodes/py-cache.git\n```\n\n## Usage\n\nJust initialize the class you want and use the public methods:\n\n```python\nfrom py_cache.contracts import Cache\nfrom py_cache.sqlite import SQLiteCache\n\n\ndef some_process_that_requires_cache(cache: Cache):\n    # retrieve the data from cache, ir the key is not cached yet and the default \n    # value is a callable the cache will use it to compute and cache the value\n    user = cache.get('user-id', lambda: db_get_user('user-id'))\n\n    print(user)\n\n\n# inject the desired cache adapter.\ncache = SQLiteCache('/tmp', 900)\nsome_process_that_requires_cache(cache)\n```\n\nChecking the if the key exists in the cache.\n\n```python\ncache.has('user-id')\n```\n\nGetting a value from the cache.\n\n```python\n# will return None if the value not exists in the cache.\ncache.get('user-id')\n\n# if the key is not present, the default value will be used.\ncache.get('user-id', {'id': 'user-id', 'email': 'vincent.vega@mail.com'})\n\n# if the default value is a callable the cache will use the callable to \n# compute and cache the request value.\ncache.get('user-id', lambda: db_get_user('user-id'))\n\n# you can also provide custom ttl (time to live) for a computed value by \n# returning a tuple of computed value and the ttl integer (Tuple[Any, int]).\ncache.get('user-id', lambda: (db_get_user('user-id'), 3600))\n```\n\nStoring value by key in cache.\n\n```python\n# store the given value by key.\ncache.put('user-id', {'id': 'user-id', 'email': 'vincent.vega@mail.com'})\n\n# store the given value with custom ttl (time to live).\ncache.put('user-id', {'id': 'user-id', 'email': 'vincent.vega@mail.com'}, 3600)\n```\n\nDelete a value from cache by key.\n\n```python\ncache.delete('user-id')\n```\n\nFlush the complete cache.\n\n```python\ncache.flush()\n```\n\n## Adapters\n\n| Adapter          | Description                                |\n|------------------|--------------------------------------------|\n| FileSystemCache  | Uses simple json files to store the cache. |\n| SQLiteCache      | SQLite will be used as cache.              |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fothercodes%2Fpy-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fothercodes%2Fpy-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fothercodes%2Fpy-cache/lists"}