Class: Silver::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/silver/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, time_field, redis_options = {}, &query) ⇒ Cache

Returns a new instance of Cache.



67
68
69
70
71
72
73
74
75
# File 'lib/silver/cache.rb', line 67

def initialize(key,time_field,redis_options={},&query)

    @key = key
    @time_field = time_field
    @query = query
    @r = Redis.new(redis_options)
    @r.select 12

end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



42
43
44
# File 'lib/silver/cache.rb', line 42

def key
  @key
end

#queryObject (readonly)

Returns the value of attribute query.



42
43
44
# File 'lib/silver/cache.rb', line 42

def query
  @query
end

#time_fieldObject (readonly)

Returns the value of attribute time_field.



42
43
44
# File 'lib/silver/cache.rb', line 42

def time_field
  @time_field
end

Instance Method Details

#cull(length, unique_key = nil, return_results = false) ⇒ Object

A helper method to keep the redis list at a reasonable size.

length is the number of entries to reduce the redis to



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/silver/cache.rb', line 126

def cull(length,unique_key=nil,return_results=false)
    
    if unique_key
      cull_dupes_by(unique_key)
    end
    @r.ltrim(@key,0,length-1)
    if return_results
      final_results = @r.lrange(@key,0,-1).map{|q| JSON.parse(q)}
      final_results = final_results.map do |result| 
          result.to_bare
      end
    else
      nil
    end
end

#find(update = true, &callback) ⇒ Object

Queries Redis, returns new entries and inserts them into Redis.

update is an optional parameter that, if false, will just return cached results and not look for new ones. callback is block that gets called for every new results, receives the result and returns the hash to be cached. This can used to query associations.

Example to cache and the query the database and include any categories the entry might have:

cache.find do |entry| 
  attrs = entry.attributes
  cats = {:categories => entry.categories}
  attrs.merge cats
end


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/silver/cache.rb', line 91

def find(update=true,&callback)
  
  old_results = @r.lrange(@key,0,-1).map{|q| JSON.parse(q)}
 
  if update
    last_date = @r.get("#{@key}:last") || "1970-01-01"
    new_results = @query.call(DateTime.parse(last_date))
    results = new_results.map do |result| 
      callback.call(result)
    end 

    if results.empty?
        final_results = old_results
    else 
        write_new(results)
        
        # Why do we go back to Redis here instead of just merging old and new? Because it's faster and cleaner than 
        # selectively determining which types are changed by the to_json (like Dates) and which are preservered (like
        # Hashes).

        final_results = @r.lrange(@key,0,-1).map{|q| JSON.parse(q)}
    end
  else
    final_results = old_results
  end
  
  final_results = final_results.map do |result| 
      result.to_bare
  end
end