def test_deco_persistent_property(): # cache_key set - cached with it class Foo(object): cache_key = "foo-cache" @persistent_property def bar(self): """Get a bar man""" return "Baz" assert isinstance(Foo.bar, persistent_property) assert Foo.bar.__doc__ == "Get a bar man" foo = Foo() assert foo.bar == "Baz" assert get_cache().get('foo-cache/bar') == "Baz" # cached version this time assert foo.bar == "Baz" # cache_key set with custom key attr - cached with it class Foo(object): special_key = "special-foo-cache" def _bar(self): return "Baz" bar = persistent_property(_bar, key_attr="special_key") foo = Foo() assert foo.bar == "Baz" assert get_cache().get('special-foo-cache/_bar') == "Baz" # cache_key set with custom key attr and name - cached with it class Foo(object): special_key = "special-foo-cache" def _bar(self): return "Baz" bar = persistent_property( _bar, name="bar", key_attr="special_key") foo = Foo() assert foo.bar == "Baz" assert get_cache().get('special-foo-cache/bar') == "Baz"
from rq.job import JobStatus, Job, loads, dumps from rq.utils import utcnow from pootle.core.cache import get_cache from pootle.core.log import log from pootle.core.url_helpers import get_all_pootle_paths, split_pootle_path from pootle.core.utils.timezone import datetime_min from pootle_misc.util import dictsum POOTLE_DIRTY_TREEITEMS = 'pootle:dirty:treeitems' POOTLE_REFRESH_STATS = 'pootle:refresh:stats' POOTLE_STATS_LAST_JOB_PREFIX = "pootle:stats:lastjob:" POOTLE_STATS_JOB_PARAMS_PREFIX = "pootle:stats:job.params:" logger = logging.getLogger('stats') cache = get_cache('stats') def statslog(function): @wraps(function) def _statslog(instance, *args, **kwargs): start = datetime.now() result = function(instance, *args, **kwargs) end = datetime.now() logger.info("%s(%s)\t%s\t%s" % (function.__name__, ', '.join(args), end - start, instance.get_cachekey())) return result return _statslog
from pootle.core.cache import get_cache from pootle.core.log import log from pootle.core.url_helpers import get_all_pootle_paths, split_pootle_path from pootle.core.utils.timezone import datetime_min from pootle_misc.util import dictsum POOTLE_DIRTY_TREEITEMS = 'pootle:dirty:treeitems' POOTLE_REFRESH_STATS = 'pootle:refresh:stats' POOTLE_STATS_LAST_JOB_PREFIX = "pootle:stats:lastjob:" POOTLE_STATS_JOB_PARAMS_PREFIX = "pootle:stats:job.params:" logger = logging.getLogger('stats') cache = get_cache('stats') def statslog(function): @wraps(function) def _statslog(instance, *args, **kwargs): start = datetime.now() result = function(instance, *args, **kwargs) end = datetime.now() logger.info("%s(%s)\t%s\t%s" % (function.__name__, ', '.join(args), end - start, instance.get_cachekey())) return result return _statslog class NoCachedStats(Exception):
def test_deco_persistent_property(): # cache_key set - cached with it class Foo(object): cache_key = "foo-cache" @persistent_property def bar(self): """Get a bar man""" return "Baz" assert isinstance(Foo.bar, persistent_property) assert Foo.bar.__doc__ == "Get a bar man" foo = Foo() assert foo.bar == "Baz" assert get_cache("lru").get('pootle.core..foo-cache.bar') == "Baz" # cached version this time assert foo.bar == "Baz" # cache_key set with custom key attr - cached with it class Foo(object): special_key = "special-foo-cache" def _bar(self): return "Baz" bar = persistent_property(_bar, key_attr="special_key") foo = Foo() assert foo.bar == "Baz" assert get_cache("lru").get('pootle.core..special-foo-cache._bar') == "Baz" # cache_key set with custom key attr and name - cached with it class Foo(object): special_key = "special-foo-cache" def _bar(self): return "Baz" bar = persistent_property( _bar, name="bar", key_attr="special_key") foo = Foo() assert foo.bar == "Baz" assert get_cache("lru").get('pootle.core..special-foo-cache.bar') == "Baz" # cache_key set with custom namespace class Foo(object): ns = "pootle.foo" cache_key = "foo-cache" @persistent_property def bar(self): """Get a bar man""" return "Baz" foo = Foo() assert foo.bar == "Baz" assert get_cache("lru").get('pootle.foo..foo-cache.bar') == "Baz" # cached version this time assert foo.bar == "Baz" # cache_key set with custom namespace and sw_version class Foo(object): ns = "pootle.foo" cache_key = "foo-cache" sw_version = "0.2.3" @persistent_property def bar(self): """Get a bar man""" return "Baz" foo = Foo() assert foo.bar == "Baz" assert get_cache("lru").get('pootle.foo.0.2.3.foo-cache.bar') == "Baz" # cached version this time assert foo.bar == "Baz"
from django_rq.queues import get_connection, get_queue from pootle.core.cache import get_cache from pootle.core.url_helpers import get_all_pootle_paths, split_pootle_path from pootle.core.utils.timezone import datetime_min from pootle_misc.util import dictsum __all__ = ("TreeItem", "CachedTreeItem", "CachedMethods") KEY_DIRTY_TREEITEMS = "pootle:dirty:treeitems" KEY_REFRESH_STATS = "pootle:refresh:stats" KEY_STATS_LAST_JOB_PREFIX = "pootle:stats:lastjob:" KEY_STATS_JOB_PARAMS_PREFIX = "pootle:stats:job.params:" logger = logging.getLogger("stats") cache = get_cache("stats") class NoCachedStats(Exception): pass class CachedMethods(object): """Cached method names.""" CHECKS = "get_checks" WORDCOUNT_STATS = "get_wordcount_stats" LAST_ACTION = "get_last_action" SUGGESTIONS = "get_suggestion_count" MTIME = "get_mtime" LAST_UPDATED = "get_last_updated"
from pootle.core.url_helpers import get_all_pootle_paths, split_pootle_path from pootle.core.utils.timezone import datetime_min from pootle_misc.util import dictsum __all__ = ("TreeItem", "CachedTreeItem", "CachedMethods") POOTLE_DIRTY_TREEITEMS = "pootle:dirty:treeitems" POOTLE_REFRESH_STATS = "pootle:refresh:stats" POOTLE_STATS_LAST_JOB_PREFIX = "pootle:stats:lastjob:" POOTLE_STATS_JOB_PARAMS_PREFIX = "pootle:stats:job.params:" logger = logging.getLogger("stats") cache = get_cache("stats") def statslog(function): @wraps(function) def _statslog(instance, *args, **kwargs): start = datetime.now() result = function(instance, *args, **kwargs) end = datetime.now() logger.info("%s(%s)\t%s\t%s", function.__name__, ", ".join(args), end - start, instance.get_cachekey()) return result return _statslog class NoCachedStats(Exception):