-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* First approach on how to implement cache strategies * Added unit tests for checking calls * Integrated with default policy for POC * POC demonstrating LRUCache plus tests * Added policy support in mget and mset
- Loading branch information
Showing
22 changed files
with
396 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import logging | ||
|
||
from collections import deque | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class DefaultPolicy: | ||
""" | ||
Default and base policy. It's the default used by all backends and it does nothing. | ||
:param client: Backend class to interact with the storage. | ||
""" | ||
|
||
def __init__(self, client): | ||
self.client = client | ||
|
||
async def pre_get(self, key): | ||
pass | ||
|
||
async def post_get(self, key): | ||
pass | ||
|
||
async def pre_set(self, key, value): | ||
pass | ||
|
||
async def post_set(self, key, value): | ||
pass | ||
|
||
|
||
class LRUPolicy(DefaultPolicy): | ||
""" | ||
Implements a Least Recently Used policy with max_keys. The policy does the following: | ||
- When a key is retrieved (get or mget), keys are moved to the beginning of the queue | ||
- When a key is added (set, mset or add), keys are added to the beginning of the queue. If | ||
the queue is full, it will remove as many keys as needed to make space for the new | ||
ones. | ||
""" | ||
def __init__(self, *args, max_keys=None, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
if max_keys is not None: | ||
assert max_keys >= 1, "Number of keys must be 1 or bigger" | ||
self.dq = deque(maxlen=max_keys) | ||
|
||
async def post_get(self, key): | ||
""" | ||
Remove the key from its current position and set it at the beginning of the queue. | ||
:param key: string key used in the get operation | ||
""" | ||
self.dq.remove(key) | ||
self.dq.appendleft(key) | ||
|
||
async def post_set(self, key, value): | ||
""" | ||
Set the given key at the beginning of the queue. If the queue is full, remove the last | ||
item first. | ||
:param key: string key used in the set operation | ||
:param value: obj used in the set operation | ||
""" | ||
if len(self.dq) == self.dq.maxlen: | ||
await self.client.delete(self.dq.pop()) | ||
self.dq.appendleft(key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ Contents | |
|
||
backends | ||
serializers | ||
policies | ||
decorators | ||
|
||
Indices and tables | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Policies | ||
======== | ||
|
||
Policies can be used to change the behavior of the cache. By default any backend uses :class:`aiocache.policies.DefaultPolicy` which does nothing. You can select the policy by calling ``cache.set_policy(MyPolicy)``. | ||
|
||
|
||
DefaultPolicy | ||
------------- | ||
|
||
.. autoclass:: aiocache.policies.DefaultPolicy | ||
:members: | ||
:undoc-members: | ||
|
||
|
||
LRUPolicy | ||
--------- | ||
|
||
.. autoclass:: aiocache.policies.LRUPolicy | ||
:members: | ||
:undoc-members: | ||
|
||
|
||
An example usage of the policy: | ||
|
||
.. literalinclude:: ../examples/policy.py | ||
:language: python | ||
:linenos: |
Oops, something went wrong.