class Fastentry::Cache

Public Class Methods

for(cache) click to toggle source
# 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

Public Instance Methods

expiration_for(key) click to toggle source
# 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_keys() click to toggle source
# File lib/fastentry.rb, line 9
def number_of_keys
  keys.size
end
read(key) click to toggle source
# File lib/fastentry.rb, line 29
def read(key)
  cache.read(key)
rescue StandardError
  nil
end
select(from: 0, amount: 20) click to toggle source
# File lib/fastentry.rb, line 13
def select(from: 0, amount: 20)
  count = adjusted_amount(from, amount)
  keys.try(:[], from, count) || []
end

Private Instance Methods

adjusted_amount(from, amount) click to toggle source
# File lib/fastentry.rb, line 67
def adjusted_amount(from, amount)
  from + amount > number_of_keys ? (number_of_keys - from) : amount
end