summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJay Hutchinson <jlhutch@gmail.com>2011-07-15 22:40:30 -0500
committerJay Hutchinson <jlhutch@gmail.com>2011-07-15 22:40:30 -0500
commited07f179faa63514985bc437dd1f19d989fd47bc (patch)
treebda64f75aa7ece5eabc11c063711fcd3867eb2f6
parentbe8a13836720f75ce77efdeeaceef7f884f22aaa (diff)
downloadpylru-ed07f179faa63514985bc437dd1f19d989fd47bc.tar.gz
Added keys(), values(), items(), and flush() to lruwrap class.
-rw-r--r--pylru.py38
1 files changed, 37 insertions, 1 deletions
diff --git a/pylru.py b/pylru.py
index 4760b4a..f135d9f 100644
--- a/pylru.py
+++ b/pylru.py
@@ -3,7 +3,7 @@
# Cache implementaion with a Least Recently Used (LRU) replacement policy and a
# basic dictionary interface.
-# Copyright (C) 2006, 2009, 2010 Jay Hutchinson
+# Copyright (C) 2006, 2009, 2010, 2011 Jay Hutchinson
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
@@ -367,6 +367,38 @@ class lruwrap(object):
except KeyError:
pass
+ def __iter__(self):
+ return self.keys()
+
+ def keys(self):
+ if self.writeback:
+ for key in self.store.keys():
+ if key not in self.dirty:
+ yield key
+
+ for key in self.dirty:
+ yield key
+ else:
+ for key in self.store.keys():
+ yield key
+
+ def values(self):
+ for key, value in self.items():
+ yield value
+
+ def items(self):
+ if self.writeback:
+ for key, value in self.store.items():
+ if key not in self.dirty:
+ yield (key, value)
+
+ for key in self.dirty:
+ value = self.cache.peek(key)
+ yield (key, value)
+ else:
+ for item in self.store.items():
+ yield item
+
def sync(self):
if self.writeback:
for key in self.dirty:
@@ -374,6 +406,10 @@ class lruwrap(object):
self.store[key] = value
self.dirty.clear()
+ def flush(self):
+ self.sync()
+ self.cache.clear()
+
def __enter__(self):
return self