-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cacheObject: caching strategies introduced IV
- Loading branch information
1 parent
00306ff
commit c503b4c
Showing
8 changed files
with
63 additions
and
73 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,2 +1,3 @@ | ||
from OSMPythonTools.cachingStrategy.json import CachingStrategyJSON | ||
from OSMPythonTools.cachingStrategy.pickle import CachingStrategyPickle | ||
from OSMPythonTools.cachingStrategy.json import JSON | ||
from OSMPythonTools.cachingStrategy.pickle import Pickle | ||
from OSMPythonTools.cachingStrategy.strategy import CachingStrategy |
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,12 @@ | ||
class CachingStrategyBase: | ||
def __init__(self): | ||
pass | ||
|
||
def get(self, key): | ||
raise(NotImplementedError('Subclass should implement get')) | ||
|
||
def set(self, key, data): | ||
raise(NotImplementedError('Subclass should implement set')) | ||
|
||
def close(self): | ||
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
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 |
---|---|---|
@@ -1,12 +1,15 @@ | ||
class CachingStrategy: | ||
def __init__(self): | ||
pass | ||
from OSMPythonTools.cachingStrategy import JSON | ||
|
||
def get(self, key): | ||
raise(NotImplementedError('Subclass should implement get')) | ||
|
||
def set(self, key, data): | ||
raise(NotImplementedError('Subclass should implement set')) | ||
|
||
def close(self): | ||
pass | ||
class CachingStrategy(): | ||
__strategy = JSON() | ||
@classmethod | ||
def use(cls, strategy, **kwargs): | ||
cls.__strategy.close() | ||
cls.__strategy = strategy(**kwargs) | ||
return cls.__strategy | ||
@classmethod | ||
def get(cls, key): | ||
return cls.__strategy.get(key) | ||
@classmethod | ||
def set(cls, key, value): | ||
cls.__strategy.set(key, value) |
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 |
---|---|---|
@@ -1,24 +1,24 @@ | ||
import pytest | ||
from OSMPythonTools import cachingStrategy | ||
|
||
from OSMPythonTools.api import Api | ||
from OSMPythonTools.cachingStrategy import CachingStrategyJSON, CachingStrategyPickle | ||
from OSMPythonTools.cachingStrategy import CachingStrategy, JSON, Pickle | ||
|
||
@pytest.mark.parametrize(('cachingStrategyF'), [ | ||
lambda: CachingStrategyJSON.instance(), | ||
lambda: CachingStrategyPickle.instance(), | ||
lambda: CachingStrategyPickle.instance(gzip=False) | ||
lambda: CachingStrategy.use(JSON), | ||
lambda: CachingStrategy.use(Pickle), | ||
lambda: CachingStrategy.use(Pickle, gzip=False), | ||
]) | ||
def test_cache(cachingStrategyF): | ||
cachingStrategy = cachingStrategyF() | ||
api = Api(cachingStrategy=cachingStrategy) | ||
api = Api() | ||
x = api.query('node/42467507') | ||
y = api.query('node/42467507') | ||
cachingStrategy.close() | ||
api = Api(cachingStrategy=cachingStrategy) | ||
api = Api() | ||
z = api.query('node/42467507') | ||
for a in [x, y, z]: | ||
assert a.version() >= 5 | ||
assert float(a.cacheVersion()) >= 1 | ||
assert x.cacheTimestamp() == y.cacheTimestamp() | ||
assert x.cacheTimestamp() == z.cacheTimestamp() | ||
CachingStrategy.use(JSON) |