Class: Taupe::Cache

Inherits:
Object
  • Object
show all
Includes:
Accessorized
Defined in:
lib/taupe/cache.rb,
lib/taupe/cache/redis.rb,
lib/taupe/cache/memcached.rb

Overview

Cache class Manage cache connection and serve as query proxy

Defined Under Namespace

Classes: MemcachedDriver, RedisDriver

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Accessorized

included

Constructor Details

#initialize(&block) ⇒ Cache

Constructor

Parameters:

  • block (Proc)

    A given block



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

def initialize(&block)
  instance_eval(&block)
end

Instance Attribute Details

#driverObject

Accessors



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

def driver
  @driver
end

#instanceObject

Accessors



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

def instance
  @instance
end

Class Method Details

.driver_factoryObject

Setup the connection driver



69
70
71
72
73
# File 'lib/taupe/cache.rb', line 69

def self.driver_factory
  cname = "Taupe::Cache::#{@instance._type.capitalize}Driver"
  klass = cname.split('::').reduce(Object) { |a, e| a.const_get e }
  @instance.driver = klass.new dsn
end

.dsnHash, String

Get the data source name

Returns:



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/taupe/cache.rb', line 56

def self.dsn
  case @instance._type
  when :memcached
    "#{@instance._host}:#{@instance._port}"
  when :redis
    {
      host: @instance._host.to_s,
      port: @instance._port.to_i
    }
  end
end

.get(key) ⇒ Object

Get a cache entry

Parameters:

  • key (String)

    The key to retrieve

Returns:

  • (Object)


78
79
80
# File 'lib/taupe/cache.rb', line 78

def self.get(key)
  @instance.driver.get key.to_s
end

.set(key, value) ⇒ Object

Set a cache entry

Parameters:

  • key (String)

    The key to set

  • value (Object)

    The value



85
86
87
# File 'lib/taupe/cache.rb', line 85

def self.set(key, value)
  @instance.driver.set key.to_s, value
end

.setup(&block) ⇒ Object

Setup the Cache instance

Parameters:

  • block (Proc)

    A given block



32
33
34
35
36
# File 'lib/taupe/cache.rb', line 32

def self.setup(&block)
  @instance = new(&block)
  setup_defaults
  driver_factory
end

.setup_defaultsObject

Setup default values



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/taupe/cache.rb', line 39

def self.setup_defaults
  case @instance._type
  when :memcached
    Taupe.require_gem 'memcached', 'Memcached cache engine'
    @instance._host ||= :localhost
    @instance._port ||= 11_211
  when :redis
    Taupe.require_gem 'redis', 'Redis cache engine'
    @instance._host ||= :localhost
    @instance._port ||= 6379
  else
    fail 'Unknown cache type'
  end
end

Instance Method Details

#delete(key) ⇒ Object

Delete a key

Parameters:

  • key (String)

    The key to delete



91
92
93
# File 'lib/taupe/cache.rb', line 91

def delete(key)
  @instance.driver.delete key.to_s
end