Skip to content

Commit

Permalink
Use asgiref local storage for backend cache (fixes #11)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcwatson committed Nov 5, 2024
1 parent 7e68135 commit 2467d6e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 3.6.0

* Switched to [uv](https://docs.astral.sh/uv/) for development
* Ensure backend cache is thread-safe (#11)


## 3.5.0

* Removed `TriggerType.snapshot` so that delete triggers will now record snapshots.
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ classifiers = [

[dependency-groups]
dev = [
"django",
"psycopg[binary]",
]

Expand Down
9 changes: 5 additions & 4 deletions src/history/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from asgiref.local import Local
from django.conf import settings
from django.db import DEFAULT_DB_ALIAS

backend_cache = {}
backend_cache = Local()


def get_backend(alias=DEFAULT_DB_ALIAS, cls=None, cache=True):
if cache and alias in backend_cache:
return backend_cache[alias]
if cache and hasattr(backend_cache, alias):
return getattr(backend_cache, alias)

if cls:
backend = cls(alias)
Expand All @@ -24,7 +25,7 @@ def get_backend(alias=DEFAULT_DB_ALIAS, cls=None, cache=True):
raise ValueError("Unsupported database engine: {}".format(engine))

if cache:
backend_cache[alias] = backend
setattr(backend_cache, alias, backend)

return backend

Expand Down
41 changes: 40 additions & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2467d6e

Please sign in to comment.