Class: SqliteCache::Store

Inherits:
ActiveSupport::Cache::Store
  • Object
show all
Defined in:
lib/sqlite_cache/store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = "", options = nil) ⇒ Store

Returns a new instance of Store.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



3
4
5
# File 'lib/sqlite_cache/store.rb', line 3

def logger
  @logger
end

Instance Method Details

#cleanup(max_time = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# 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) ⇒ Object



24
25
26
27
28
29
# 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