diff options
author | Jay Hutchinson <jlhutch@gmail.com> | 2011-07-15 22:40:30 -0500 |
---|---|---|
committer | Jay Hutchinson <jlhutch@gmail.com> | 2011-07-15 22:40:30 -0500 |
commit | ed07f179faa63514985bc437dd1f19d989fd47bc (patch) | |
tree | bda64f75aa7ece5eabc11c063711fcd3867eb2f6 | |
parent | be8a13836720f75ce77efdeeaceef7f884f22aaa (diff) | |
download | pylru-ed07f179faa63514985bc437dd1f19d989fd47bc.tar.gz |
Added keys(), values(), items(), and flush() to lruwrap class.
-rw-r--r-- | pylru.py | 38 |
1 files changed, 37 insertions, 1 deletions
@@ -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 |