Class: Fastentry::Cache

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/fastentry.rb

Direct Known Subclasses

DalliCache, DefaultCache, MemcacheCache, RedisCache

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for(cache) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fastentry.rb', line 35

def self.for(cache)
  if ::Rails::VERSION::MAJOR >= 5
    # Comparing cache name due to class require statements:
    ## ex. https://github.com/rails/rails/blob/94b5cd3a20edadd6f6b8cf0bdf1a4d4919df86cb/activesupport/lib/active_support/cache/redis_cache_store.rb#L5
    case cache.class.name
    when "ActiveSupport::Cache::FileStore"
      DefaultCache.new(cache)
    when "ActiveSupport::Cache::MemoryStore"
      DefaultCache.new(cache)
    when "ActiveSupport::Cache::RedisCacheStore"
      RedisCache.new(cache)
    when "ActiveSupport::Cache::MemCacheStore"
      MemcacheCache.new(cache)
    when "ActiveSupport::Cache::DalliStore"
      DalliCache.new(cache)
    else
      raise ArgumentError, 'Unsupported cache type'
    end
  else
    case cache.class.name
    when "ActiveSupport::Cache::MemCacheStore"
      DalliCache.new(cache)
    when "ActiveSupport::Cache::DalliStore"
      DalliCache.new(cache)
    else
      DefaultCache.new(cache)
    end
  end
end

Instance Method Details

#expiration_for(key) ⇒ Object



22
23
24
25
26
27
# File 'lib/fastentry.rb', line 22

def expiration_for(key)
  expires_at = cache.send(:read_entry, key, {}).expires_at
  expires_at.to_s.strip.empty? ? nil : Time.at(expires_at)
rescue StandardError
  nil
end

#number_of_keysObject



9
10
11
# File 'lib/fastentry.rb', line 9

def number_of_keys
  keys.size
end

#read(key) ⇒ Object



29
30
31
32
33
# File 'lib/fastentry.rb', line 29

def read(key)
  cache.read(key)
rescue StandardError
  nil
end

#search(query: '') ⇒ Object



18
19
20
# File 'lib/fastentry.rb', line 18

def search(query: '')
  keys.select! { |key| key.downcase.include?(query.downcase) } || []
end

#select(from: 0, amount: 20) ⇒ Object



13
14
15
16
# File 'lib/fastentry.rb', line 13

def select(from: 0, amount: 20)
  count = adjusted_amount(from, amount)
  keys.try(:[], from, count) || []
end