Class: CoffeeTable::Cache

Inherits:
Object
  • Object
show all
Includes:
Utility
Defined in:
lib/coffee_table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utility

#method_missing, #underscore

Constructor Details

#initialize(options = {}) ⇒ Cache

initialize for coffee_table. takes options to setup behaviour of cache



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/coffee_table.rb', line 22

def initialize(options={})
  @options = options.symbolize_keys

  default_enable_cache_to true
  default_redis_namespace_to :coffee_table
  default_redis_server_to "127.0.0.1"
  default_redis_port_to 6379
  default_redis_to nil
  default_ignore_code_changes_to false
  default_compress_content_to true
  default_compress_min_size_to 10240

  redis_client = nil
  if !@options[:redis].nil?
    redis_client = @options[:redis]
  elsif @options.has_key?(:redis_url)
    redis_client = Redis.new(:url => @options[:redis_url])
  else
    redis_client = Redis.new(:host => @options[:redis_server], :port => @options[:redis_port])
  end

  @redis = Redis::Namespace.new(@options[:redis_namespace], :redis => redis_client)
  @real_redis = redis_client

  self

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class CoffeeTable::Utility

Instance Attribute Details

#redisObject (readonly)

Returns the value of attribute redis.



19
20
21
# File 'lib/coffee_table.rb', line 19

def redis
  @redis
end

Instance Method Details

#expire_allObject



116
117
118
# File 'lib/coffee_table.rb', line 116

def expire_all
  keys.map{|key| expire_key(key)}
end

#expire_for(*objects) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/coffee_table.rb', line 124

def expire_for(*objects)
  if defined? Rails
    perform_caching = Rails.application.configure do
      config.action_controller.perform_caching
    end
  else
    perform_caching = true
  end

  if perform_caching
    deleted_keys = []
    unless objects.count == 0
      keys.map{|k| CoffeeTable::Key.parse(k)}.each do |key|
        expire = true
        objects.each do |object|
          if object.class == String || object.class == Symbol
            unless key.has_element?(object)
              expire = false
            end
          elsif object.class == Class
            object_type = underscore(object.to_s)
            unless key.has_element_type?(object_type) || key.has_element_type?(ActiveSupport::Inflector.pluralize(object_type))
              expire = false
            end
          else
            object_type = underscore(object.class.to_s)
            unless key.has_element?("#{object_type.to_sym}[#{object.id}]") or key.has_element?(object_type)
              expire = false
            end
          end
        end
        if expire
          expire_key(key.to_s)
          deleted_keys << key
        end
      end
    end
    deleted_keys
  end
end

#expire_key(key_value) ⇒ Object



110
111
112
113
114
# File 'lib/coffee_table.rb', line 110

def expire_key(key_value)
  keys.map{|k| CoffeeTable::Key.parse(k)}.select{|key| key.has_element?(key_value) || key.to_s == key_value }.each do |key|
    @redis.del(key.to_s)
  end
end

#fetch(initial_key, *related_objects, &block) ⇒ Object Also known as: get_cache



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/coffee_table.rb', line 51

def fetch(initial_key, *related_objects, &block)
  raise CoffeeTable::BlockMissingError, "No block given to generate cache from" unless block_given?

  # extract the options hash if it is present
  options = {}
  if related_objects[-1].instance_of? Hash
    options = related_objects[-1]
    related_objects = related_objects[0..-2]
  end

  # check objects are valid
  related_objects.flatten.map{|o| raise CoffeeTable::InvalidObjectError, "Objects passed in must have an id method or be a class" unless object_valid?(o)}

  if @options[:ignore_code_changes]
    block_key = ""
  else
    block_source = RubyVM::InstructionSequence.disasm(block.to_proc).to_s.gsub(/\(\s*\d+\)/, "").gsub(/^== disasm.*?$/, "")
    block_key = Digest::MD5.hexdigest(block_source)
  end
  flags = {}

  # if first related_object is integer or fixnum it is used as an expiry time for the cache object
  key = CoffeeTable::Key.new({name: initial_key, block_key: block_key, options: @options, flags: flags}, related_objects)
  if @options[:enable_cache]
    if options.has_key?(:expiry)
      expiry = options[:expiry]
    else
      expiry = nil
    end
    if keys.include?(key.to_s)
      result = marshal_value(@redis.get(key.to_s))
    else
      key.add_flag(:compressed => true)
      if keys.include?(key.to_s)
        result = marshal_value(@redis.get(key.to_s)).gunzip
      else
        key.remove_flag(:compressed)
        result = yield

        compress_result = @options[:compress_content] && result.kind_of?(String) && result.length > @options[:compress_min_size]

        if compress_result
          key.add_flag(:compressed => true)
          @redis.set(key.to_s, Marshal.dump(result.gzip))
        else
          @redis.set(key.to_s, Marshal.dump(result))
        end

        unless expiry.nil?
          @redis.expire key.to_s, expiry
        end
      end
    end
  else
    result = yield
  end
  result
end

#keysObject



120
121
122
# File 'lib/coffee_table.rb', line 120

def keys
  @redis.keys
end