Class: Taupe::Cache
- Inherits:
-
Object
- Object
- Taupe::Cache
- 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
-
#driver ⇒ Object
Accessors.
-
#instance ⇒ Object
Accessors.
Class Method Summary collapse
-
.driver_factory ⇒ Object
Setup the connection driver.
-
.dsn ⇒ Hash, String
Get the data source name.
-
.get(key) ⇒ Object
Get a cache entry.
-
.set(key, value) ⇒ Object
Set a cache entry.
-
.setup(&block) ⇒ Object
Setup the Cache instance.
-
.setup_defaults ⇒ Object
Setup default values.
Instance Method Summary collapse
-
#delete(key) ⇒ Object
Delete a key.
-
#initialize(&block) ⇒ Cache
constructor
Constructor.
Methods included from Accessorized
Constructor Details
#initialize(&block) ⇒ Cache
Constructor
26 27 28 |
# File 'lib/taupe/cache.rb', line 26 def initialize(&block) instance_eval(&block) end |
Instance Attribute Details
#driver ⇒ Object
Accessors
22 23 24 |
# File 'lib/taupe/cache.rb', line 22 def driver @driver end |
#instance ⇒ Object
Accessors
22 23 24 |
# File 'lib/taupe/cache.rb', line 22 def instance @instance end |
Class Method Details
.driver_factory ⇒ Object
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 |
.dsn ⇒ Hash, String
Get the data source name
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
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
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
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_defaults ⇒ Object
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
91 92 93 |
# File 'lib/taupe/cache.rb', line 91 def delete(key) @instance.driver.delete key.to_s end |