Class: ActiveSupport::Cache::BigCache

Inherits:
Store
  • Object
show all
Defined in:
lib/big_cache.rb

Constant Summary collapse

VERSION =
'1.0.1'

Instance Method Summary collapse

Constructor Details

#initialize(bucket_name, access_key, secret_key) ⇒ BigCache

Returns a new instance of BigCache.



10
11
12
13
14
15
# File 'lib/big_cache.rb', line 10

def initialize(bucket_name, access_key, secret_key)
  puts 'Creating new BigCache'
  @s3 = RightAws::S3.new(access_key, secret_key, {:multi_thread => false, :protocol => "http", :port => 80})
  @bucket = @s3.bucket(bucket_name, true)

end

Instance Method Details

#delete(name, options = nil) ⇒ Object



62
63
64
65
# File 'lib/big_cache.rb', line 62

def delete(name, options = nil)
  super
  @bucket.key(name).delete
end

#delete_matched(matcher, options = nil) ⇒ Object



67
68
69
70
# File 'lib/big_cache.rb', line 67

def delete_matched(matcher, options = nil)
  super
  raise "delete_matched not supported by BigCache"
end

#get(key) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/big_cache.rb', line 17

def get(key)
  # if the API URL exists as a key in cache, we just return it
  # we also make sure the data is fresh
  key = @bucket.key(key)
  if !key.exists?
    return nil
  end
  cache_entry = key.data
#        puts 'cache_entry=' + cache_entry
  index = cache_entry.index('::')
 #       puts 'index=' + index.to_s
  expires = cache_entry[0..index].to_i
  #      puts 'expires in get=' + expires.to_s
  expires2 = (expires - Time.now.to_i)
  if expires2 > 0
#            puts 'returning from cache ' + @cache[key][1].inspect
    return Marshal.load(cache_entry[(index+2)...cache_entry.length])
  else
    #            puts 'expired=' + key + ' at ' + expires.to_s + ' and now=' + Time.now.to_s
    @bucket.key(key).delete # do async
  end
  return nil
end

#put(key, val, seconds_to_store = 9999999) ⇒ Object



41
42
43
44
45
46
# File 'lib/big_cache.rb', line 41

def put(key, val, seconds_to_store=9999999)
#        seconds_to_store = seconds_to_store || 9999999
   #     puts 'seconds=' + seconds_to_store.to_s
  data = Marshal.dump(val)
  @bucket.put(key, (Time.now+seconds_to_store).to_i.to_s + "::" + data)
end

#read(name, options = nil) ⇒ Object



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

def read(name, options = nil)
  #        puts 'read from localcache'
  super
  ret = get(name)
#        puts 'ret.frozen=' + ret.frozen?.to_s
  return ret
end

#write(name, value, options = nil) ⇒ Object



56
57
58
59
60
# File 'lib/big_cache.rb', line 56

def write(name, value, options = nil)
  super
  put(name, value, options.nil? ? nil : options[:expires_in])
#        puts 'write.frozen=' + value.frozen?.to_s
end