Boo
  1. Boo
  2. BOO-352

Boo.Lang.Useful.Collection.Cache

    Details

    • Type: New Feature New Feature
    • Status: Resolved Resolved
    • Priority: Minor 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 :
      1

      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

        Activity

        Hide
        Edwin de Jonge added a comment -

        I made a small typo in the name of Boo.Lang.Useful.Collection.Cache
        I forgot the "s" in Collections...

        Show
        Edwin de Jonge added a comment - I made a small typo in the name of Boo.Lang.Useful.Collection.Cache I forgot the "s" in Collections...
        Hide
        Rodrigo B. de Oliveira added a comment -

        Thanks!

        Show
        Rodrigo B. de Oliveira added a comment - Thanks!

          People

          • Assignee:
            Edwin de Jonge
            Reporter:
            Edwin de Jonge
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Time Tracking

              Estimated:
              Original Estimate - 15 minutes
              15m
              Remaining:
              Remaining Estimate - 15 minutes
              15m
              Logged:
              Time Spent - Not Specified
              Not Specified