Commits

Lynn Rees committed f265350

- adjust cull
- fix postgres import

Comments (0)

Files changed (5)

     leveldb=shove.stores.leveldbstore:LevelDBStore
     memory=shove.store:MemoryStore
     mongodb=shove.stores.mongodb:MongoDBStore
-    mssql=shove.store.db:DBStore
+    mssql=shove.stores.db:DBStore
     mysql=shove.stores.db:DBStore
     oracle=shove.stores.db:DBStore
-    postgres=shove.store.db:DBStore
+    postgres=shove.stores.db:DBStore
     redis=shove.stores.redisdb:RedisStore
     s3=shove.stores.s3:S3Store
     simple=shove.store:SimpleStore
 from shove._compat import synchronized
 from shove.base import Mapping, FileBase
 
-__all__ = [
-    'FileCache', 'FileLRUCache', 'MemoryCache', 'MemoryLRUCache',
-    'SimpleLRUCache', 'SimpleCache',
-]
+__all__ = (
+    'FileCache FileLRUCache MemoryCache SimpleCache MemoryLRUCache'
+    'SimpleLRUCache'
+).split()
 
 
 class BaseCache(object):
         return value
 
     def __setitem__(self, key, value):
-        # cull values if over max number of entries
-        if len(self) >= self._max_entries:
-            self._cull()
         # set expiration time and value
         exp = time() + self.timeout
         super(BaseCache, self).__setitem__(key, (exp, value))
+        # cull values if over max number of entries
+        if len(self) > self._max_entries:
+            self._cull()
 
     def _cull(self):
-        num = 0
-        # remove items in cache to make room
-        maxcull = self._maxcull
-        # cull number of items allowed (set by self._maxcull)
-        for num, key in enumerate(self):
-            # remove only maximum # of items allowed by maxcull
-            if num <= maxcull:
-                # remove items if expired
-                try:
-                    self[key]
-                except KeyError:
-                    num += 1
-            else:
-                break
-        choice = random.choice
-        keys = list(self)
-        max_entries = self._max_entries
-        # remove any additional items up to max # of items allowed by maxcull
-        while len(self) >= max_entries and num <= maxcull:
-            # cull remainder of allowed quota at random
-            del self[choice(keys)]
-            num += 1
+        # cull remainder of allowed quota at random
+        for key in random.sample(list(self), len(self) - self._max_entries):
+            del self[key]
 
 
 class SimpleCache(BaseCache, Mapping):
 
 from shove._imports import cache_backend, store_backend
 
-__all__ = ('Shove', 'MultiShove')
+__all__ = 'Shove MultiShove'.split()
 
 
 class Shove(MutableMapping):
 from shove.base import Mapping, FileBase
 from shove._compat import anydbm, synchronized, url2pathname
 
-__all__ = ('DBMStore', 'FileStore', 'MemoryStore', 'SimpleStore')
+__all__ = 'DBMStore FileStore MemoryStore SimpleStore'.split()
 
 
 class BaseStore(Mapping, MutableMapping):
     def test_cull(self):
         from shove._imports import cache_backend
         cache = cache_backend(self.initstring, max_entries=1)
-        cache['test'] = 'test'
-        cache['test2'] = 'test'
-        cache['test2'] = 'test'
+        cache['test1'] = 'test'
+        cache['test2'] = 'test2'
+        cache['test3'] = 'test3'
         self.assertEquals(len(cache), 1)