Lynn Rees avatar Lynn Rees committed 76a03ce

- tweak

Comments (0)

Files changed (3)

         except (IOError, OSError):
             raise KeyError(key)
 
-    def __iter__(self):
+    def __iter__(self, unquote_plus=unquote_plus):
         for name in listdir(self._dir):
             if not name.startswith('.'):
                 yield unquote_plus(name)
         return exists(self._key_to_file(key))
 
     def __len__(self):
-        return len([i for i in listdir(self._dir) if not i.startswith('.')])
+        return len(i for i in listdir(self._dir) if not i.startswith('.'))
 
     def _createdir(self):
         # creates the store directory
 # -*- coding: utf-8 -*-
 '''shove cache core.'''
 
-import random
 from time import time
 from copy import deepcopy
+from operator import delitem
 from collections import deque
+from random import seed, sample
 from threading import Condition
 
+from stuf.iterable import xpartmap
+
 from shove._compat import synchronized
 from shove.base import Mapping, FileBase
 
+
 __all__ = (
     'FileCache FileLRUCache MemoryCache SimpleCache MemoryLRUCache'
     'SimpleLRUCache'
     def __init__(self, engine, **kw):
         super(BaseCache, self).__init__(engine, **kw)
         # get random seed
-        random.seed()
+        seed()
         # set maximum number of items to cull if over max
         self._maxcull = kw.get('maxcull', 10)
         # set max entries
 
     def _cull(self):
         # cull remainder of allowed quota at random
-        for key in random.sample(list(self), len(self) - self._max_entries):
-            del self[key]
+        xpartmap(
+            delitem, sample(list(self), len(self) - self._max_entries), self
+        )
 
 
 class SimpleCache(BaseCache, Mapping):
     def __setitem__(self, key, value):
         if len(self) >= self._max_entries:
             self._cull()
-        super(FileCache, self).__setitem__(
-            key, (time() + self.timeout, value)
-        )
+        super(FileCache, self).__setitem__(key, (time() + self.timeout, value))
 
 
 class BaseLRUCache(BaseCache):
             store = self
             max_entries = self._max_entries
             refcount = self._refcount
-            delitem = super(BaseLRUCache, self).__delitem__
+            ditem = super(BaseLRUCache, self).__delitem__
+            qpopleft = queue.popleft
             while len(store) > max_entries:
-                k = queue.popleft()
+                k = qpopleft()
                 refcount[k] -= 1
                 if not refcount[k]:
-                    delitem(k)
+                    ditem(k)
                     del refcount[k]
 
     def _housekeep(self, key):
         self._refcount[key] = self._refcount.get(key, 0) + 1
         if len(self._queue) > self._max_entries * 4:
             queue = self._queue
+            qpopleft, qappend = queue.popleft, queue.append
             refcount = self._refcount
             for _ in [None] * len(queue):
-                k = queue.popleft()
+                k = qpopleft()
                 if refcount[k] == 1:
-                    queue.append(k)
+                    qappend(k)
                 else:
                     refcount[k] -= 1
 
 # -*- coding: utf-8 -*-
 '''shove core.'''
 
-from functools import partial
 from operator import methodcaller
 from collections import MutableMapping
 
 from stuf import exhaustmap
+from stuf.iterable import xpartmap
 from concurrent.futures import ThreadPoolExecutor
 
 from shove._imports import cache_backend, store_backend
 
+
 __all__ = 'Shove MultiShove'.split()
 
 
         except AttributeError:
             pass
         with ThreadPoolExecutor(max_workers=self._maxworkers) as executor:
-            exhaustmap(
-                partial(executor.submit, methodcaller('__delitem__', key)),
+            xpartmap(
+                executor.submit,
                 self._stores,
+                methodcaller('__delitem__', key),
             )
         try:
             del self._cache[key]
     def sync(self):
         '''Writes buffer to store.'''
         with ThreadPoolExecutor(max_workers=self._maxworkers) as executor:
-            exhaustmap(
-                partial(executor.submit, methodcaller('update', self._buffer)),
+            xpartmap(
+                executor.submit,
                 self._stores,
+                methodcaller('update', self._buffer),
             )
         self._buffer.clear()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.