Class: Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/cache.rb,
lib/cache/config.rb,
lib/cache/version.rb

Defined Under Namespace

Modules: ActiveSupportCacheDalliStore, ActiveSupportCacheFileStore, ActiveSupportCacheMemoryStore, ActiveSupportCacheStore, DalliClient, MemCache, Memcached, MemcachedRails, Redis, RedisNamespace Classes: Config

Constant Summary collapse

VERSION =
'0.4.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metal = nil) ⇒ Cache

:nodoc:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cache.rb', line 29

def initialize(metal = nil) #:nodoc:
  @pid = ::Process.pid
  @config = Config.new
  @metal = if metal.is_a?(Cache)
    metal.metal
  elsif metal
    metal
  elsif defined?(::Rails) and ::Rails.respond_to?(:cache) and rails_cache = ::Rails.cache
    rails_cache
  else
    require 'active_support'
    require 'active_support/cache'
    require 'active_support/cache/memory_store'
    ::ActiveSupport::Cache::MemoryStore.new
  end
  metal_class = @metal.class.name.delete('::') # Memcached::Rails -> 'MemcachedRails'
  # non-ActiveSupport underscore per http://stackoverflow.com/questions/1509915/converting-camel-case-to-underscore-case-in-ruby
  require "cache/#{metal_class.gsub(/(.)([A-Z])/,'\1_\2').downcase}"
  extend Cache.const_get(metal_class)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



23
24
25
# File 'lib/cache.rb', line 23

def config
  @config
end

#loggerObject

For compatibility with Rails 2.x



27
28
29
# File 'lib/cache.rb', line 27

def logger
  @logger
end

#metalObject (readonly)

Returns the value of attribute metal.



24
25
26
# File 'lib/cache.rb', line 24

def metal
  @metal
end

Class Method Details

.wrap(metal = nil) ⇒ Object

Create a new Cache instance by wrapping a client of your choice.

Defaults to an in-process memory store.

Supported memcached clients:

  • memcached (either a Memcached or a Memcached::Rails)

  • dalli (either a Dalli::Client or an ActiveSupport::Cache::DalliStore)

  • memcache-client (MemCache, the one commonly used by Rails)

Supported Redis clients:

Example:

raw_client = Memcached.new('127.0.0.1:11211')
cache = Cache.wrap raw_client


19
20
21
# File 'lib/cache.rb', line 19

def self.wrap(metal = nil)
  new metal
end

Instance Method Details

#cas(k, ttl = nil, &blk) ⇒ Object Also known as: compare_and_swap

Get the current value (if any), pass it into a block, and set the result.

Example:

cache.cas 'hello' { |current| 'world' }


151
152
153
154
155
156
157
158
159
# File 'lib/cache.rb', line 151

def cas(k, ttl = nil, &blk)
  handle_fork
  if blk and _exist?(k)
    old_v = _get k
    new_v = blk.call old_v
    _set k, new_v, extract_ttl(ttl)
    new_v
  end
end

#decrement(k, amount = 1, ignored_options = nil) ⇒ Object

Decrement a value.

Example:

cache.decrement 'high-fives'


126
127
128
# File 'lib/cache.rb', line 126

def decrement(k, amount = 1, ignored_options = nil)
  increment k, -amount
end

#delete(k, ignored_options = nil) ⇒ Object

Delete a value.

Example:

cache.delete 'hello'


86
87
88
89
# File 'lib/cache.rb', line 86

def delete(k, ignored_options = nil)
  handle_fork
  _delete k
end

#exist?(k, ignored_options = nil) ⇒ Boolean

Check if something exists.

Example:

cache.exist? 'hello'

Returns:

  • (Boolean)


106
107
108
109
# File 'lib/cache.rb', line 106

def exist?(k, ignored_options = nil)
  handle_fork
  _exist? k
end

#fetch(k, ttl = nil, &blk) ⇒ Object

Try to get a value and if it doesn’t exist, set it to the result of the block.

Accepts :expires_in for compatibility with Rails.

Example:

cache.fetch 'hello' { 'world' }


136
137
138
139
140
141
142
143
144
145
# File 'lib/cache.rb', line 136

def fetch(k, ttl = nil, &blk)
  handle_fork
  if _exist? k
    _get k
  elsif blk
    v = blk.call
    _set k, v, extract_ttl(ttl)
    v
  end
end

#flushObject Also known as: clear

Flush the cache.

Example:

cache.flush


95
96
97
98
# File 'lib/cache.rb', line 95

def flush
  handle_fork
  _flush
end

#get(k, ignored_options = nil) ⇒ Object Also known as: read

Get a value.

Example:

cache.get 'hello'


54
55
56
57
# File 'lib/cache.rb', line 54

def get(k, ignored_options = nil)
  handle_fork
  _get k
end

#get_multi(*ks) ⇒ Object

Get multiple cache entries.

Example:

cache.get_multi 'hello', 'privyet'


65
66
67
68
# File 'lib/cache.rb', line 65

def get_multi(*ks)
  handle_fork
  _get_multi ks
end

#increment(k, amount = 1, ignored_options = nil) ⇒ Object

Increment a value.

Example:

cache.increment 'high-fives'


115
116
117
118
119
120
# File 'lib/cache.rb', line 115

def increment(k, amount = 1, ignored_options = nil)
  handle_fork
  new_v = _get(k).to_i + amount
  _set k, new_v, 0
  new_v
end

#set(k, v, ttl = nil, ignored_options = nil) ⇒ Object Also known as: write

Store a value. Note that this will Marshal it.

Example:

cache.set 'hello', 'world'
cache.set 'hello', 'world', 80 # seconds til it expires


75
76
77
78
# File 'lib/cache.rb', line 75

def set(k, v, ttl = nil, ignored_options = nil)
  handle_fork
  _set k, v, extract_ttl(ttl)
end

#statsObject

Get stats.

Example:

cache.stats


167
168
169
170
# File 'lib/cache.rb', line 167

def stats
  handle_fork
  _stats
end