# HG changeset patch # User Marcin Kuzminski # Date 2018-08-05 22:35:46 # Node ID ec5716e4ab85c6c480ee7ba214b2f3f7db96b54a # Parent 2f16baeeb2b3715797c9d8cb2f472e2c04863576 caches: use repo.lru based Dict cache. This LRUDict uses Timing Algo to not have to use locking for the LRU implementation, this it's safer to use for dogpile. We used it before with beaker, so it's generally more stable. diff --git a/pkgs/python-packages.nix b/pkgs/python-packages.nix --- a/pkgs/python-packages.nix +++ b/pkgs/python-packages.nix @@ -791,17 +791,6 @@ self: super: { license = [ pkgs.lib.licenses.bsdOriginal ]; }; }; - "lru-dict" = super.buildPythonPackage { - name = "lru-dict-1.1.6"; - doCheck = false; - src = fetchurl { - url = "https://files.pythonhosted.org/packages/00/a5/32ed6e10246cd341ca8cc205acea5d208e4053f48a4dced2b1b31d45ba3f/lru-dict-1.1.6.tar.gz"; - sha256 = "1k2lhd4dpl6xa6iialbwx4l6bkdzxmzhygms39pvf19x1rk5fm1n"; - }; - meta = { - license = [ pkgs.lib.licenses.mit ]; - }; - }; "lxml" = super.buildPythonPackage { name = "lxml-3.7.3"; doCheck = false; @@ -1690,7 +1679,6 @@ self: super: { self."billiard" self."kombu" self."lxml" - self."lru-dict" self."mako" self."markdown" self."markupsafe" diff --git a/requirements.txt b/requirements.txt --- a/requirements.txt +++ b/requirements.txt @@ -31,7 +31,6 @@ jinja2==2.9.6 billiard==3.5.0.3 kombu==4.2.0 lxml==3.7.3 -lru-dict==1.1.6 mako==1.0.7 markdown==2.6.11 markupsafe==1.0.0 diff --git a/rhodecode/lib/memory_lru_debug.py b/rhodecode/lib/memory_lru_dict.py rename from rhodecode/lib/memory_lru_debug.py rename to rhodecode/lib/memory_lru_dict.py --- a/rhodecode/lib/memory_lru_debug.py +++ b/rhodecode/lib/memory_lru_dict.py @@ -25,15 +25,8 @@ and the state of LRU dict. inrae.cache is licensed under LRUDict is licensed under ZPL license This software is Copyright (c) Zope Corporation (tm) and Contributors. All rights reserved. - -TODO: marcink, we might think of replacing the LRUDict with lru-dict library -written in C. +""" -eg difference in speed: - - LRUDictC Time : 0.00025 s, Memory : 110592 Kb - LRUDict Time : 0.00369 s, Memory : 147456 Kb -""" import logging from repoze.lru import LRUCache @@ -44,8 +37,10 @@ log = logging.getLogger(__name__) class LRUDict(LRUCache): - """ Wrapper to provide partial dict access """ + Wrapper to provide partial dict access + """ + def __setitem__(self, key, value): return self.put(key, value) diff --git a/rhodecode/lib/rc_cache/backends.py b/rhodecode/lib/rc_cache/backends.py --- a/rhodecode/lib/rc_cache/backends.py +++ b/rhodecode/lib/rc_cache/backends.py @@ -28,7 +28,8 @@ from dogpile.cache.backends import file from dogpile.cache.backends import redis as redis_backend from dogpile.cache.backends.file import NO_VALUE, compat, FileLock from dogpile.cache.util import memoized_property -from lru import LRU as LRUDict + +from rhodecode.lib.memory_lru_dict import LRUDict, LRUDictDebug _default_max_size = 1024 @@ -41,14 +42,12 @@ class LRUMemoryBackend(memory_backend.Me def __init__(self, arguments): max_size = arguments.pop('max_size', _default_max_size) - callback = None - if arguments.pop('log_max_size_reached', None): - def evicted(key, value): - log.debug( - 'LRU: evicting key `%s` due to max size %s reach', key, max_size) - callback = evicted - arguments['cache_dict'] = LRUDict(max_size, callback=callback) + LRUDictClass = LRUDict + if arguments.pop('log_key_count', None): + LRUDictClass = LRUDictDebug + + arguments['cache_dict'] = LRUDictClass(max_size) super(LRUMemoryBackend, self).__init__(arguments) def delete(self, key): @@ -111,9 +110,9 @@ class CustomLockFactory(FileLock): # waited to much time on a lock, better fail than loop for ever log.error('Failed to acquire lock on %s file', self.filename) raise - - log.debug('Failed to acquire lock, retry in 0.03') - gevent.sleep(0.03) + timeout = 0.03 + log.debug('Failed to acquire lock, retry in %ss', timeout) + gevent.sleep(timeout) fcntl.flock = gevent_flock return fcntl diff --git a/setup.py b/setup.py --- a/setup.py +++ b/setup.py @@ -182,8 +182,8 @@ setup( 'rc-ssh-wrapper=rhodecode.apps.ssh_support.lib.ssh_wrapper:main', ], 'beaker.backends': [ - 'memorylru_base=rhodecode.lib.memory_lru_debug:MemoryLRUNamespaceManagerBase', - 'memorylru_debug=rhodecode.lib.memory_lru_debug:MemoryLRUNamespaceManagerDebug' + 'memorylru_base=rhodecode.lib.memory_lru_dict:MemoryLRUNamespaceManagerBase', + 'memorylru_debug=rhodecode.lib.memory_lru_dict:MemoryLRUNamespaceManagerDebug' ] }, )