Class: CacheManager::Key

Inherits:
Object
  • Object
show all
Defined in:
app/models/cache_manager/key.rb

Constant Summary collapse

EXCLUDED_DIRS =
['.', '..', 'assets'].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeKey

Returns a new instance of Key.



7
8
9
# File 'app/models/cache_manager/key.rb', line 7

def initialize
  @klass = Rails.cache.class.to_s
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



5
6
7
# File 'app/models/cache_manager/key.rb', line 5

def klass
  @klass
end

Class Method Details

.allObject



13
14
15
# File 'app/models/cache_manager/key.rb', line 13

def all
  new.all
end

.destroy(id) ⇒ Object



29
30
31
# File 'app/models/cache_manager/key.rb', line 29

def destroy(id)
  Rails.cache.delete(id)
end

.find(id) ⇒ Object



25
26
27
# File 'app/models/cache_manager/key.rb', line 25

def find(id)
  Rails.cache.read(id)
end

.flush(id) ⇒ Object



17
18
19
20
21
22
23
# File 'app/models/cache_manager/key.rb', line 17

def flush(id)
  if id == 'all'
    Rails.cache.clear
  else
    Rails.cache.delete(id)
 end
end

Instance Method Details

#allObject



34
35
36
37
38
39
40
41
# File 'app/models/cache_manager/key.rb', line 34

def all
  case @klass
  when 'ActiveSupport::Cache::FileStore'
    keys_for_file_store
  else
    keys_for_memory_store
  end
end

#keys_for_file_storeObject



47
48
49
50
51
52
53
54
# File 'app/models/cache_manager/key.rb', line 47

def keys_for_file_store
  cache_path = Rails.cache.instance_variable_get(:'@cache_path')
  root_dirs = Dir.entries(cache_path).reject { |f| (EXCLUDED_DIRS + ['.gitkeep']).include?(f) }
  full_paths = root_dirs.collect { |i| cache_path + "#{i}" }
  full_paths.collect do |dir|
    File.basename(Dir["#{dir}/*/*"][0]) if Dir["#{dir}/*/*"][0]
  end
end

#keys_for_memory_storeObject



43
44
45
# File 'app/models/cache_manager/key.rb', line 43

def keys_for_memory_store
  Rails.cache.instance_variable_get(:@data).keys
end