Class: CaChing::Adapters::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/ca_ching/adapters/redis.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Redis

Returns a new instance of Redis.



4
5
6
# File 'lib/ca_ching/adapters/redis.rb', line 4

def initialize(options={})
  @cache = ::Redis.new(options)
end

Instance Method Details

#clear!Object



82
83
84
# File 'lib/ca_ching/adapters/redis.rb', line 82

def clear!
  @cache.flushdb
end

#deflate(object, options = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ca_ching/adapters/redis.rb', line 105

def deflate(object, options={})
  attributes = if options[:old_values]
    object.attributes.keys.inject({}) do |attributes, key|
      attributes[key] = object.send(:"#{key}_was")
      attributes
    end
  else
    object.attributes
  end
  
  ActiveSupport::JSON.encode(attributes)
end

#deflate_with_score(objects, options = {}) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/ca_ching/adapters/redis.rb', line 118

def deflate_with_score(objects, options={})
  return nil if objects.nil?
  
  score_method = options[:sorted_by] || :id
  
  objects.map { |object| [object.send(score_method).to_i, deflate(object)] }
end

#destroy(object, options = {}) ⇒ Object



78
79
80
# File 'lib/ca_ching/adapters/redis.rb', line 78

def destroy(object, options={})
  @cache.zrem(options[:at], deflate(object, :old_values => options[:old_values]))
end

#find(query, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ca_ching/adapters/redis.rb', line 8

def find(query, options={})
  results = nil        
  # where_values in the form { :field => ['=', 'value'] }
  where_values = query.where
  keys = where_values.map { |where_value| "#{where_value[0]}#{where_value[1][0]}#{where_value[1][1]}" }
  return nil if keys.empty?
  
  offset = query.offset || 0
  limit = (query.limit || 0) + offset - 1
  
  if keys.length <= 1
    key = "#{query.table_name}:#{keys[0]}"
    return nil unless @cache.exists key # the key has never been added to the cache, so it's a miss
    
    results = @cache.zrange(key, offset, limit)
  else # needs an intersection
    intersection_key = "#{query.table_name}:#{keys.join "&"}"
    
    if @cache.exists intersection_key
      results = @cache.zrange(intersection_key, offset, limit)
    else
      return nil unless keys.inject(true) { |memo, key| @cache.exists("#{query.table_name}:#{key}") && memo }
      @cache.zinterstore intersection_key, *keys.map { |key| "#{query.table_name}:#{key}" }
      @cache.zrange(intersection_key, offset, limit)
      @cache.del(intersection_key) unless options[:keep_intersection]
    end
  end
  
  inflate(results, :for => query)
end

#inflate(objects, options = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ca_ching/adapters/redis.rb', line 86

def inflate(objects, options={})
  return nil if objects.nil?
  
  unless options[:for]
    return objects
  else
    objects.map do |object|
      obj = options[:for].klass.new
      attributes = ActiveSupport::JSON.decode(object)
      attributes.each do |attr, value|
        obj.send(:"#{attr}=", value)
      end
      obj.changed_attributes.clear
      obj.send(:instance_variable_set, "@new_record", false)
      obj
    end
  end
end

#insert(objects, options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ca_ching/adapters/redis.rb', line 39

def insert(objects, options={})
  key = nil
  if options[:for]
    query = options[:for]
    if (where_values = query.where).length == 1
      where_value = query.where.first
      key = "#{query.table_name}:#{where_value[0]}#{where_value[1][0]}#{where_value[1][1]}"
    else
      nil
    end
  elsif options[:at]
    key = options[:at]
  end
  
  return nil if key.nil?
  
  deflated = deflate_with_score(objects)
  deflated.each do |object_and_score|
    @cache.zadd key, *object_and_score
  end

  objects
end

#update(object, options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ca_ching/adapters/redis.rb', line 63

def update(object, options={})
  old_keys = object.to_keys(:old_values => true).select { |key| @cache.exists(key) }
  new_keys = object.to_keys(:old_values => false).select { |key| @cache.exists(key) }
  
  @cache.multi do
    old_keys.each do |key|
      destroy(object, :at => key, :old_values => true)
    end

    new_keys.each do |key|
      insert([object], :at => key)
    end
  end
end