-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d43a6cd
commit 22dea4e
Showing
9 changed files
with
199 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,72 @@ | ||
import json | ||
import os.path | ||
from os import makedirs, getenv | ||
from os.path import dirname, exists, realpath, expanduser, expandvars | ||
|
||
from SimpleCache2.CacheSystem import CacheSystem | ||
|
||
|
||
class Settings(CacheSystem): | ||
settings_file: str | ||
|
||
def __init__(self, settings_file: str = None): | ||
self.settings_file = expanduser(expandvars(getenv('DISK_CACHE_DIR', os.path.join(dirname(realpath(__file__)), 'disk_cache', "settings.json")))) | ||
self.settingsData = {} | ||
if settings_file: | ||
self.settings_file = settings_file | ||
makedirs(dirname(self.settings_file), exist_ok=True) | ||
super().__init__() | ||
self.load() | ||
|
||
def __del__(self): | ||
self.save() | ||
pass | ||
|
||
def __set__(self, instance, value): | ||
self.set(instance, value) | ||
|
||
def __delete___(self, instance): | ||
self.delete(instance) | ||
|
||
def __len__(self): | ||
return len(self.settingsData) | ||
|
||
def exist(self, key: any) -> bool: | ||
k = self.getKey(key) | ||
return k in self.settingsData | ||
pass | ||
|
||
def get(self, key: any) -> any: | ||
k = self.getKey(key) | ||
if k in self.settingsData: | ||
return self.settingsData[k] | ||
return None | ||
pass | ||
|
||
def set(self, key: any, value: object, ttl: int = 0) -> object: | ||
k = self.getKey(key) | ||
self.settingsData[k] = value | ||
self.save() | ||
return self | ||
pass | ||
|
||
def delete(self, key: any) -> bool: | ||
return False | ||
pass | ||
|
||
def clearOld(self) -> list: | ||
return [] | ||
pass | ||
|
||
def load(self): | ||
if not exists(self.settings_file): | ||
self.save() | ||
return | ||
with open(self.settings_file, "rb") as file: | ||
self.settingsData = json.load(file) | ||
pass | ||
|
||
def save(self): | ||
with open(self.settings_file, "w") as file: | ||
json.dump(self.settingsData, file) | ||
pass |
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 |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
|
||
setuptools.setup( | ||
name="SimpleCache2", | ||
version="1.1.0", | ||
version="1.2.0", | ||
author="Traineratwot", | ||
author_email="[email protected]", | ||
description="Cache system for python", | ||
|
@@ -18,4 +18,4 @@ | |
"License :: OSI Approved :: MIT License", | ||
"Operating System :: OS Independent", | ||
), | ||
) | ||
) |
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,56 @@ | ||
import unittest | ||
from time import sleep | ||
|
||
from SimpleCache2 import simple_cache | ||
from SimpleCache2.MemoryCache import MemoryCache | ||
|
||
|
||
class MemoryCacheTest(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.cache = MemoryCache() | ||
|
||
def test(self): | ||
self.cache.set('test1', (1, 2), 10) | ||
g = self.cache.get('test1') | ||
self.assertEqual((1, 2), g) | ||
|
||
def test2(self): | ||
self.cache.set('test2', [1, 2], 10) | ||
g = self.cache.get('test2') | ||
self.assertEqual([1, 2], g) | ||
|
||
def test3(self): | ||
self.cache.set('test3', {"hello": "world"}, 10) | ||
g = self.cache.get('test3') | ||
self.assertEqual({"hello": "world"}, g) | ||
|
||
def test4(self): | ||
self.cache.set('test4', "string", 10) | ||
g = self.cache.get('test4') | ||
self.assertEqual("string", g) | ||
|
||
def test5(self): | ||
def testFunc(name): | ||
sleep(1) | ||
return f"hello world {name}" | ||
|
||
g = self.cache.call('test5', 10, testFunc, 'Bob') | ||
self.assertEqual("hello world Bob", g) | ||
|
||
g = self.cache.call('test5', 10, testFunc, 'Bob') | ||
self.assertEqual("hello world Bob", g) | ||
pass | ||
|
||
def test6Decorator(self): | ||
@simple_cache(self.cache, 10) | ||
def testFunc(name): | ||
sleep(1) | ||
return f"hello world {name}" | ||
|
||
self.assertEqual(testFunc('Bob'), testFunc('Bob')) | ||
self.assertNotEqual(testFunc('Bob'), testFunc('Dan')) | ||
pass | ||
|
||
def test7(self): | ||
self.assertNotEqual("/test/bob/index.php", self.cache.getKey('/test/bob/index.php')) |
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