Class: FindCache::KeyGen

Inherits:
Object
  • Object
show all
Defined in:
lib/find_cache/key_gen.rb

Class Method Summary collapse

Class Method Details

.cache_key(model_name, id) ⇒ Object

cache_key generator for model items

Parameters

  • model_name - The name of the model

  • id - Id for the given model

Examples

FindCache::KeyGen.cache_key("User", 1)


16
17
18
# File 'lib/find_cache/key_gen.rb', line 16

def self.cache_key(model_name, id)
  "#{model_name}/#{id}"
end

.cache_key_ref(model_name, foreign_key_name, foreign_key_id) ⇒ Object

cache_key generator for model items with referenced attribute

Parameters

  • model_name - The name of the model

  • foreign_key_name - Name of the foreign_key attribute

  • foreign_key_id - Id for the given foreign_key name

Examples

FindCache::KeyGen.cache_key_ref("UserDetail", "user_id", 1)


31
32
33
# File 'lib/find_cache/key_gen.rb', line 31

def self.cache_key_ref(model_name, foreign_key_name, foreign_key_id)
  "#{model_name}/#{foreign_key_name}-#{foreign_key_id}"
end

.clear_find_cache_storeObject

Cleaning all caches of the current Thread not the Rails.cache



58
59
60
61
# File 'lib/find_cache/key_gen.rb', line 58

def self.clear_find_cache_store
  $find_cache_store.delete(KeyGen.global_cache_key)
  KeyGen.global_cache_key(true)
end

.global_cache_key(clear = false) ⇒ Object

cache_key generator for the current thread

Parameters

  • clear - Boolean value to clean up the current Thread’s cache key

Examples

# For generating cache key for the current Thread
FindCache::KeyGen.global_cache_key
# For cleaning generated cache key for the current Thread
FindCache::KeyGen.global_cache_key(true)


47
48
49
50
51
52
53
54
# File 'lib/find_cache/key_gen.rb', line 47

def self.global_cache_key(clear = false)
  return (Thread.current[:global_cache_key] = nil) if clear
  Thread.current[:global_cache_key] ||= (
    t = SimpleUUID::UUID.new(Time.now).to_i.to_s(16).rjust(32, "0")
    $find_cache_store[t] = {}
    t
  )
end