Class: ObjectCache

Inherits:
Object
  • Object
show all
Defined in:
lib/object_cache/version.rb,
lib/object_cache/cache_store.rb,
lib/object_cache/rails_store.rb,
lib/object_cache/class_cacher.rb,
lib/object_cache/memory_store.rb,
lib/object_cache/object_cache.rb,
lib/object_cache/object_reader.rb

Defined Under Namespace

Classes: CacheStore, ClassCacher, MemoryStore, RailsStore, Reader

Constant Summary collapse

VERSION =
"0.0.7"

Class Method Summary collapse

Class Method Details

.cache_storeObject



22
23
24
# File 'lib/object_cache/object_cache.rb', line 22

def self.cache_store
  @@cache_store
end

.cache_store=(store) ⇒ Object



26
27
28
# File 'lib/object_cache/object_cache.rb', line 26

def self.cache_store=(store)
  @@cache_store = store
end

.constantize(camel_cased_word) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/object_cache/object_cache.rb', line 50

def self.constantize(camel_cased_word)
  names = camel_cased_word.split('::')
  names.shift if names.empty? || names.first.empty?

  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end

.expire_after(ttl) ⇒ Object



14
15
16
# File 'lib/object_cache/object_cache.rb', line 14

def self.expire_after(ttl)
  @@ttl = ttl
end

.method_missing(method_name) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/object_cache/object_cache.rb', line 61

def self.method_missing(method_name)
  if cache_store.hash_empty? || @@expires_at.to_i < Time.now.to_i
    refresh_the_cache
  end

  cache_store.hash_get(method_name.to_s)
end

.refresh_the_cacheObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/object_cache/object_cache.rb', line 30

def self.refresh_the_cache
  @@reader ||= Reader.new(@@file_or_hash)

  groups = @@reader.grouped_objects
  groups.each do |klass, ids|
    #we need both ids and items to be in the same order for this to work
    ids.sort!{|a,b| a.last <=> b.last}
    items = constantize(klass).find(ids.collect(&:last))
    items.sort!{|a,b| a.id <=> b.id}

    @i = 0
    items.each do |item|
      cache_store.hash_set(ids[@i].first.to_s, item)
      @i+=1
    end
    
  end
  @@expires_at = Time.now + (@@ttl || 300)
end

.source(file_or_hash) ⇒ Object



10
11
12
# File 'lib/object_cache/object_cache.rb', line 10

def self.source(file_or_hash)
  @@file_or_hash = file_or_hash
end

.ttlObject



18
19
20
# File 'lib/object_cache/object_cache.rb', line 18

def self.ttl
  @@ttl
end