Class: SqliteCache::Store
- Inherits:
-
ActiveSupport::Cache::Store
- Object
- ActiveSupport::Cache::Store
- SqliteCache::Store
- Defined in:
- lib/sqlite_cache/store.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
- #cleanup(max_time = nil) ⇒ Object
- #clear(options = nil) ⇒ Object
-
#initialize(path = "", options = nil) ⇒ Store
constructor
A new instance of Store.
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 = "", = nil) @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
#logger ⇒ Object
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], ) delete_entry(row[:key], ) 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( = nil) @data.delete rescue Sequel::Error => e logger.error("Sequel::Error (#{e}): #{e.}") if logger nil end |