Details
-
Type:
New Feature
-
Status:
Resolved
-
Priority:
Minor
-
Resolution: Fixed
-
Affects Version/s: 0.5
-
Fix Version/s: 0.6
-
Component/s: Boo.Lang.Useful
-
Labels:None
-
Testcase included:yes
-
Number of attachments :
Description
I implemented a cache class for the MemoizeAttribute (which may be renamed to CacheAttribute if that is nicer), but I think it is useful by itself. I also added a CacheFixture class
It works like a Hash with a limited size. The least recently used item is removed when size overflows.
The cache class has a creator method that will be used if an entry is not found in cache. You can supply this method to the cache class (you don't have to inherit from it).
Furthermore it can fire an event if an entry is removed from cache due to overflow.
Usage
cache = Cache(2) #create a cache with size 2
cache.Add(1, "1")
cache.Add(2, "2")
cache.Add(3, "3") #overflow, least recently used item will be removed i.e. 1, "1"
assert cache.Count == 2
assert cache[3] == "3"
assert cache[2] == "2"
cache.Add(4,"4") #overflow, 3 will be removed (since 2 is most recently used)
assert cache.Count == 2
assert cache[4] == "4"
assert cache[2] == "2"
#add a creator method
cache.Creator = def(key):
print 'Create entry: ' + key
return key.ToString()
#add an removed event
cache.Removed += def(key):
print "Removed " + key
assert cache[42] == "42" #will print: Create entry: 42. Removed 4 from cache
I made a small typo in the name of Boo.Lang.Useful.Collection.Cache
I forgot the "s" in Collections...