From ed07f179faa63514985bc437dd1f19d989fd47bc Mon Sep 17 00:00:00 2001 From: Jay Hutchinson Date: Fri, 15 Jul 2011 22:40:30 -0500 Subject: Added keys(), values(), items(), and flush() to lruwrap class. --- pylru.py | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) 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 -- cgit v1.2.1