lib/DataCache.rb in taskjuggler-0.0.8 vs lib/DataCache.rb in taskjuggler-0.0.9
- old
+ new
@@ -1,11 +1,12 @@
#!/usr/bin/env ruby -w
# encoding: UTF-8
#
# = DataCache.rb -- The TaskJuggler III Project Management Software
#
-# Copyright (c) 2006, 2007, 2008, 2009, 2010 by Chris Schlaeger <cs@kde.org>
+# Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011
+# by Chris Schlaeger <chris@linux.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
@@ -105,9 +106,22 @@
@hits += 1
e.value
else
@misses += 1
nil
+ end
+ end
+
+ # If we have a value for the _key_, return the value. Otherwise call the
+ # block to compute the value, store it and return it.
+ def cached(key)
+ if @entries.has_key?(key)
+ e = @entries[key]
+ @hits += 1
+ e.value
+ else
+ @misses += 1
+ store(yield, key)
end
end
def to_s
<<"EOT"