Class: TwitterRedisIdentityMap::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter-redis-identitymap/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Base

Returns a new instance of Base.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
# File 'lib/twitter-redis-identitymap/base.rb', line 5

def initialize(options = nil)
	raise ArgumentError if !options.nil? && !options.is_a?(Hash)
	@options = options.nil? ? {} : options
	@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

Raises:

  • (ArgumentError)


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

#redisObject



29
30
31
# File 'lib/twitter-redis-identitymap/base.rb', line 29

def redis
	@redis
end

#store(attrs, object) ⇒ Object

Raises:

  • (ArgumentError)


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