1# Cache
2
3A simple cache using the database.
4
5The Plain Cache stores JSON-serializable values in a `CachedItem` model.
6Cached data can be set to expire after a certain amount of time.
7
8Access to the cache is provided through the `Cached` class.
9
10```python
11from plain.cache import Cached
12
13
14cached = Cached("my-cache-key")
15
16if cached.exists():
17 print("Cache hit and not expired!")
18 print(cached.value)
19else:
20 print("Cache miss!")
21 cached.set("a JSON-serializable value", expiration=60)
22
23# Delete the item if you need to
24cached.delete()
25```
26
27Expired cache items can be cleared by [running chores](/plain/plain/chores/README.md).
28
29## Installation
30
31Add `plain.cache` to your `INSTALLED_PACKAGES`:
32
33```python
34# app/settings.py
35INSTALLED_PACKAGES = [
36 # ...
37 "plain.cache",
38]
39```
40
41## CLI
42
43- `plain cache clear-expired` - Clear all expired cache items
44- `plain cache clear-all` - Clear all cache items
45- `plain cache stats` - Show cache statistics