-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from mrf345/testing
Add caching module and refactor code
- Loading branch information
Showing
8 changed files
with
200 additions
and
151 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
name: Build | ||
on: [push] | ||
on: | ||
push: | ||
schedule: | ||
# runs a new build every saturday | ||
- cron: 0 0 * * 6 | ||
|
||
jobs: | ||
build: | ||
|
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,44 @@ | ||
from flask_minify.utils import get_optimized_hashing | ||
|
||
|
||
class MemoryCache: | ||
def __init__(self, store_key_getter=None, limit=0): | ||
self.store_key_getter = store_key_getter | ||
self.limit = limit | ||
self._cache = {} | ||
self.hashing = get_optimized_hashing() | ||
|
||
@property | ||
def store(self): | ||
if self.store_key_getter: | ||
return self._cache.setdefault(self.store_key_getter(), {}) | ||
|
||
return self._cache | ||
|
||
@property | ||
def limit_exceeded(self): | ||
return len(self.store) >= self.limit | ||
|
||
def __getitem__(self, key): | ||
return self.store.get(key) | ||
|
||
def __setitem__(self, key, value): | ||
if self.limit_exceeded: | ||
self.store.popitem() | ||
|
||
self.store.update({key: value}) | ||
|
||
def get_or_set(self, key, getter): | ||
if self.limit == 0: | ||
return getter() | ||
|
||
hashed_key = self.hashing(key.encode("utf-8")).hexdigest() | ||
|
||
if not self[hashed_key]: | ||
self[hashed_key] = getter() | ||
|
||
return self[hashed_key] | ||
|
||
def clear(self): | ||
del self._cache | ||
self._cache = {} |
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
Oops, something went wrong.