Class: Marfa::Cache
- Inherits:
-
Object
- Object
- Marfa::Cache
- Defined in:
- lib/marfa/cache.rb
Overview
Redis-cache wrapper
Instance Method Summary collapse
-
#create_json_key(path) ⇒ String
Create key for json urls.
-
#create_key(kind, path, tags) ⇒ String
Create key by params.
-
#delete(key) ⇒ Object
Delete data from cache.
-
#delete_by_pattern(pattern) ⇒ Object
Delete data from cache by pattern.
-
#exist?(key) ⇒ Boolean
Check that key exist in cache.
-
#get(key) ⇒ String, Nil
Get data from cache.
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
-
#set(key, value, _time = @config[:expiration_time]) ⇒ Object
Write data to cache.
Constructor Details
Instance Method Details
#create_json_key(path) ⇒ String
Create key for json urls
73 74 75 |
# File 'lib/marfa/cache.rb', line 73 def create_json_key(path) path.gsub(%r{[/.]}, '_') end |
#create_key(kind, path, tags) ⇒ String
Create key by params
64 65 66 |
# File 'lib/marfa/cache.rb', line 64 def create_key(kind, path, ) kind + '_' + path.tr('/', '_') + '__' + .join('_') end |
#delete(key) ⇒ Object
Delete data from cache
44 45 46 |
# File 'lib/marfa/cache.rb', line 44 def delete(key) @redis.del(key) end |
#delete_by_pattern(pattern) ⇒ Object
Delete data from cache by pattern
52 53 54 55 |
# File 'lib/marfa/cache.rb', line 52 def delete_by_pattern(pattern) keys = @redis.keys("*#{pattern}*") @redis.del(*keys) unless keys.empty? end |
#exist?(key) ⇒ Boolean
Check that key exist in cache
35 36 37 38 |
# File 'lib/marfa/cache.rb', line 35 def exist?(key) return unless @config[:enabled] @redis.exists(key) end |
#get(key) ⇒ String, Nil
Get data from cache
26 27 28 |
# File 'lib/marfa/cache.rb', line 26 def get(key) @redis.get(key) end |
#set(key, value, _time = @config[:expiration_time]) ⇒ Object
Write data to cache
15 16 17 18 |
# File 'lib/marfa/cache.rb', line 15 def set(key, value, _time = @config[:expiration_time]) return unless @config[:enabled] @redis.set(key, value) end |