Class: Scat::Cache

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

Overview

The session and global key => value cache wrapper.

Instance Method Summary collapse

Instance Method Details

#add(tag, value) ⇒ Object

Adds the given value to the cache tag set. The value is converted to a string, if necessary.

Parameters:

  • tag (String, Symbol)

    the cache tag

  • value

    the value to add



35
36
37
# File 'lib/scat/cache.rb', line 35

def add(tag, value)
  datastore.zadd(tag, 0, value)
end

#datastoreRedis

Returns the Redis cache data store.

Returns:

  • (Redis)

    the Redis cache data store

Raises:

  • (ScatError)

    if the cache could not be started



8
9
10
# File 'lib/scat/cache.rb', line 8

def datastore
  @redis ||= discover
end

#get(tag, key = nil) ⇒ String+

Returns the matching value or values.

Parameters:

  • tag (String, Symbol)

    the cache tag

  • the (Symbol, nil)

    cached tag hash key

Returns:

  • (String, <String>)

    the matching value or values



42
43
44
# File 'lib/scat/cache.rb', line 42

def get(tag, key=nil)
  key ? datastore.hget(tag, key) : datastore.get(tag)
end

#get_all(tag) ⇒ <String>

Returns the matching set.

Parameters:

  • tag (String, Symbol)

    the cache tag

Returns:

  • (<String>)

    the matching set



48
49
50
# File 'lib/scat/cache.rb', line 48

def get_all(tag)
  datastore.zrange(tag, 0, -1)
end

#set(tag, value, key = nil) ⇒ Object

Sets the given value to the cache tag set as follows:

  • If the key is nil, then the cache entry is the tag.

  • Otherwise, the cache entry is the tag hash entry for the given key.

  • If the value is nil, then the entry is removed from the cache.

  • Otherwise, the value is converted to a string, if necessary, and the cache entry is set to the value.

Parameters:

  • tag (String, Symbol)

    the cache tag

  • value

    the value to set

  • key (String, Symbol, nil) (defaults to: nil)

    the cache tag hash key



22
23
24
25
26
27
28
# File 'lib/scat/cache.rb', line 22

def set(tag, value, key=nil)
  if value.nil? then
    key ? datastore.hdel(tag, key) : datastore.rem(tag) 
  else
    key ? datastore.hset(tag, key, value) : datastore.set(tag, value)
  end
end