Class: ActiveSupport::Cache::ActiveRecordStore

Inherits:
Store
  • Object
show all
Defined in:
lib/active_support/cache/active_record_store.rb

Defined Under Namespace

Classes: CacheItem

Constant Summary collapse

VERSION =
"0.0.3"
ITEMS_LIMIT =

do not allow to store more items than

5000

Instance Method Summary collapse

Instance Method Details

#clearObject



53
54
55
# File 'lib/active_support/cache/active_record_store.rb', line 53

def clear
  CacheItem.delete_all
end

#debug_mode?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/active_support/cache/active_record_store.rb', line 84

def debug_mode?
  ENV['ACTIVE_RECORD_CACHE_STORE_DEBUG_MODE'] == "1"
end

#delete_entry(key, options) ⇒ Object



57
58
59
# File 'lib/active_support/cache/active_record_store.rb', line 57

def delete_entry(key, options)
  CacheItem.delete_all(:key => key)
end

#read_entry(key, options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/active_support/cache/active_record_store.rb', line 61

def read_entry(key, options={})
  item = CacheItem.find_by_key(key)

  if item.present? && debug_mode?
    item.meta_info[:access_counter] += 1
    item.meta_info[:access_time] = Time.now
    item.save
  end

  item
end

#write_entry(key, entry, options) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/active_support/cache/active_record_store.rb', line 73

def write_entry(key, entry, options)
  options = options.clone.symbolize_keys
  item = CacheItem.find_or_initialize_by_key(key)
  item.debug_mode = debug_mode?
  item.value = entry.value
  item.expires_at = options[:expires_in].try(:since)
  item.save

  free_some_space
end