Class: TimedCache

Inherits:
Object
  • Object
show all
Defined in:
lib/timedcache.rb

Overview

TimedCache

TimedCache implements a cache in which you can place objects and specify a timeout value.

If you attempt to retrieve the object within the specified timeout period, the object will be returned. If the timeout period has elapsed, the TimedCache will return nil.

e.g.:

cache = TimedCache.new
cache.put :my_object_key, "Expensive data", 10 # => "Expensive data"

cache.get :my_object_key # => "Expensive data"
cache[:my_object_key]    # => "Expensive data"

… 10 seconds later:

cache.get :my_object_key # => nil

Default timeout

When creating a new TimedCache, a default timeout value can be set. This value will be used for each object added to the cache, unless a different timeout value is specifically set for that object.

e.g.:

cache = TimedCache.new(:default_timeout => 120)
cache.default_timeout # => 120

File-based cache

By default, TimedCache will use an in-memory store. A file-based store (using the PStore library) can also be used.

e.g.:

TimedCache.new(:type => :file, :filename => "my_cache.db")

The file-based cache makes it possible to easily share a cache between several ruby processes. However when using the cache in this way, you will probably want to update the cache by passing a block to the TimedCache#get method (see below for details).

Note that objects that cannot be marshalled (e.g. a Proc) can’t be stored using the file-based cache.

Defined Under Namespace

Classes: FileStore, MemoryStore, ObjectContainer, Store

Constant Summary collapse

VERSION =
"0.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ TimedCache

Create a new TimedCache. Available options are:

type

:memory or :file (defaults to :memory).

default_timeout

Timeout to use if none is specified when adding an object to the cache.

filename

Must be specified when using the :file type store.

e.g.:

TimedCache.new(:type => :file, :filename => "cache.db")


60
61
62
63
64
65
# File 'lib/timedcache.rb', line 60

def initialize(opts = {})
  opts[:type] ||= :memory
  @default_timeout = opts[:default_timeout] || 60
  @store = new_store(opts)
  @store.extend(MonitorMixin)
end

Instance Attribute Details

#default_timeoutObject (readonly)

Returns the value of attribute default_timeout.



51
52
53
# File 'lib/timedcache.rb', line 51

def default_timeout
  @default_timeout
end

Instance Method Details

#[](key) ⇒ Object

Fetch objects using the hash syntax. e.g.:

cache[:name] # => "Nick"


108
109
110
# File 'lib/timedcache.rb', line 108

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object

Add to the cache using a hash-like syntax. e.g.:

cache[:name] = "Nick"

Note that adding to the cache this way does not allow you to specify timeout values on a per-object basis.



102
103
104
# File 'lib/timedcache.rb', line 102

def []=(key, value)
  put(key, value)
end

#get(key, &block) ⇒ Object

Retrieve the object which the given key. If the object has expired or is not present, nil is returned.

Optionally, a block can be given. The result of evaluating the block will be substituted as the cache value, if the cache has expired.

e.g.:

cache.get("slow_database_query") do
  MyDatabase.query("SELECT * FROM bigtable...")
end

The block syntax can also be used with the in-memory cache.



91
92
93
94
95
# File 'lib/timedcache.rb', line 91

def get(key, &block)
  @store.synchronize do
    @store.get(key, &block)
  end
end

#put(key, value, timeout = @default_timeout) ⇒ Object

Add an object to the cache. e.g.:

cache.put(:session_id, 12345)

The third parameter is an optional timeout value. If not specified, the :default_timeout for this TimedCache will be used instead.



72
73
74
75
76
# File 'lib/timedcache.rb', line 72

def put(key, value, timeout = @default_timeout)
  @store.synchronize do
    @store.put(key, value, timeout) unless value.nil?
  end
end