Class: TwitterRedisIdentityMap::Base
- Inherits:
-
Object
- Object
- TwitterRedisIdentityMap::Base
- Defined in:
- lib/twitter-redis-identitymap/base.rb
Instance Method Summary collapse
- #fetch(id) ⇒ Object
-
#initialize(options = nil) ⇒ Base
constructor
A new instance of Base.
- #redis ⇒ Object
- #store(attrs, object) ⇒ Object
Constructor Details
#initialize(options = nil) ⇒ Base
Returns a new instance of Base.
5 6 7 8 9 10 |
# File 'lib/twitter-redis-identitymap/base.rb', line 5 def initialize( = nil) raise ArgumentError if !.nil? && !.is_a?(Hash) @options = .nil? ? {} : @options[:namespace] = "tri:cache:" unless @options[:namespace] @redis = @options[:redis].nil? ? Redis.new : Redis.new({:url => @options[:redis]}) end |
Instance Method Details
#fetch(id) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/twitter-redis-identitymap/base.rb', line 12 def fetch(id) raise ArgumentError if id.nil? id = Marshal.load(id) object = nil if @redis.exists("#{@options[:namespace]}#{id[:id]}") val = @redis.get("#{@options[:namespace]}#{id[:id]}") object = Marshal.load(val) end object end |
#redis ⇒ Object
29 30 31 |
# File 'lib/twitter-redis-identitymap/base.rb', line 29 def redis @redis end |
#store(attrs, object) ⇒ Object
23 24 25 26 27 |
# File 'lib/twitter-redis-identitymap/base.rb', line 23 def store(attrs, object) raise ArgumentError if (attrs.nil? || object.nil?) attrs = Marshal.load(attrs) @redis.set("#{@options[:namespace]}#{attrs[:id]}", Marshal.dump(object)) == "OK" ? object : nil end |