Skip to content

Commit

Permalink
Merge pull request #62 from zodb/issuo61
Browse files Browse the repository at this point in the history
Use with statements. Fixes #61
  • Loading branch information
jamadden committed Jun 20, 2016
2 parents 55db0a1 + 7b415c7 commit 05acab2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 124 deletions.
22 changes: 4 additions & 18 deletions relstorage/cache.py
Expand Up @@ -632,8 +632,6 @@ class LocalClient(object):

def __init__(self, options):
self._lock = threading.Lock()
self._lock_acquire = self._lock.acquire
self._lock_release = self._lock.release
self._bucket_limit = int(1000000 * options.cache_local_mb / 2)
self._value_limit = options.cache_local_object_max
self._bucket0 = LocalClientBucket(self._bucket_limit)
Expand All @@ -650,21 +648,17 @@ def __init__(self, options):
self._decompress = module.decompress

def flush_all(self):
self._lock_acquire()
try:
with self._lock:
self._bucket0 = LocalClientBucket(self._bucket_limit)
self._bucket1 = LocalClientBucket(self._bucket_limit)
finally:
self._lock_release()

def get(self, key):
return self.get_multi([key]).get(key)

def get_multi(self, keys):
res = {}
decompress = self._decompress
self._lock_acquire()
try:
with self._lock:
for key in keys:
cvalue = self._bucket0.get(key)
if cvalue is None:
Expand All @@ -685,8 +679,6 @@ def get_multi(self, keys):
value = cvalue

res[key] = value
finally:
self._lock_release()
return res

def _set_one(self, key, cvalue):
Expand Down Expand Up @@ -716,8 +708,7 @@ def set_multi(self, d, allow_replace=True):
# don't bother
return
compress = self._compress
self._lock_acquire()
try:
with self._lock:
for key, value in iteritems(d):
# XXX PY3 Shouldn't we assert isinstance(bytes)?
# Why do we allow non-bytes values? Do we use them outside
Expand Down Expand Up @@ -745,8 +736,6 @@ def set_multi(self, d, allow_replace=True):
del self._bucket1[key]

self._set_one(key, cvalue)
finally:
self._lock_release()

def add(self, key, value):
self.set_multi({key: value}, allow_replace=False)
Expand All @@ -756,8 +745,7 @@ def incr(self, key):
# don't bother
return None
decompress = self._decompress
self._lock_acquire()
try:
with self._lock:
cvalue = self._bucket0.get(key)
if cvalue is None:
cvalue = self._bucket1.get(key)
Expand All @@ -777,5 +765,3 @@ def incr(self, key):
res = int(value) + 1
self._set_one(key, res)
return res
finally:
self._lock_release()

0 comments on commit 05acab2

Please sign in to comment.