class SqliteCache::Store

Attributes

logger[RW]

Public Class Methods

new(path = "", options = nil) click to toggle source
# File lib/sqlite_cache/store.rb, line 5
def initialize(path = "", options = nil)
  @options = options ? options.dup : {}
  @logger = @options[:logger]
  @max_cleanup_time = @options.fetch(:max_prune_time, 2)

  if path.present?
    @db = Sequel.connect("sqlite://#{path}")
  else
    @db = Sequel.sqlite
  end

  @db.create_table(:cache) do
    String :key
    String :value
  end unless @db.table_exists?(:cache)

  @data = @db[:cache]
end

Public Instance Methods

cleanup(max_time = nil) click to toggle source
# File lib/sqlite_cache/store.rb, line 31
def cleanup(max_time = nil)
  instrument(:cleanup, size: @data.count) do
    start_time = Time.now
    @data.each do |row|
      entry = read_entry(row[:key], options)
      delete_entry(row[:key], options) if entry && entry.expired?
      return if (max_time && Time.now - start_time > max_time)
    end
  end
end
clear(options = nil) click to toggle source
# File lib/sqlite_cache/store.rb, line 24
def clear(options = nil)
  @data.delete
rescue Sequel::Error => e
  logger.error("Sequel::Error (#{e}): #{e.message}") if logger
  nil
end

Protected Instance Methods

count() click to toggle source
# File lib/sqlite_cache/store.rb, line 44
def count
  @data.count
end

Private Instance Methods

deserialize_entry(raw_value) click to toggle source
# File lib/sqlite_cache/store.rb, line 88
def deserialize_entry(raw_value)
  if raw_value
    entry = Marshal.load(raw_value) rescue raw_value
    entry.is_a?(ActiveSupport::Cache::Entry) ? entry : ActiveSupport::Cache::Entry.new(entry)
  else
    nil
  end
end
serialize_entry(value) click to toggle source
# File lib/sqlite_cache/store.rb, line 79
def serialize_entry(value)
  if value
    entry = Marshal.dump(value) rescue value
    entry.is_a?(Entry) ? entry : Entry.new(entry)
  else
    nil
  end
end