Class: Marfa::Cache
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 = nil) ⇒ Object
Write data to cache.
Constructor Details
#initialize ⇒ Cache
Returns a new instance of Cache.
7 8 9 10 |
# File 'lib/marfa/cache.rb', line 7 def initialize @config = Marfa.config.cache @redis = Redis.new(host: @config[:host], port: @config[:port], db: @config[:db]) end |
Instance Method Details
#create_json_key(path) ⇒ String
Create key for json urls
79 80 81 |
# File 'lib/marfa/cache.rb', line 79 def create_json_key(path) path.gsub(%r{[/.]}, '_') end |
#create_key(kind, path, tags = []) ⇒ String
Create key by params
70 71 72 |
# File 'lib/marfa/cache.rb', line 70 def create_key(kind, path, = []) kind + '_' + path.tr('/', '_') + '__' + .join('_') end |
#delete(key) ⇒ Object
Delete data from cache
50 51 52 |
# File 'lib/marfa/cache.rb', line 50 def delete(key) @redis.del(key) end |
#delete_by_pattern(pattern) ⇒ Object
Delete data from cache by pattern
58 59 60 61 |
# File 'lib/marfa/cache.rb', line 58 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
41 42 43 44 |
# File 'lib/marfa/cache.rb', line 41 def exist?(key) return unless @config[:enabled] @redis.exists(key) end |
#get(key) ⇒ String, Nil
Get data from cache
32 33 34 |
# File 'lib/marfa/cache.rb', line 32 def get(key) @redis.get(key) end |
#set(key, value, _time = nil) ⇒ Object
Write data to cache
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/marfa/cache.rb', line 15 def set(key, value, _time = nil) return unless @config[:enabled] return if _time.zero? if _time.is_a? Numeric @redis.set(key, value, ex: _time) # ex - is seconds else @redis.set(key, value) #infinite end end |