Created
December 18, 2011 00:41
-
-
Save flexd/1491946 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "cinch/storage" | |
require "redis" | |
require "json" | |
module Cinch | |
class Storage | |
class Redis < Storage | |
def initialize(options, plugin) | |
# New Redis object, thread safe by default since 2.2.0 | |
@redis = ::Redis.new(:host => 'localhost', :port => 6379) | |
@options = options | |
@base = plugin.class.plugin_name | |
@debug = true | |
# Do we need a mutex like the yaml plugin? Thread safe and all. | |
end | |
def serialize(object) | |
puts "Serializing #{object.inspect}" unless !@debug | |
return object.to_json | |
end | |
def unserialize(object) | |
puts "Unserializing #{object.inspect}" unless !@debug | |
return JSON.parse(object) | |
end | |
def has_key?(key) | |
puts "Do we have the key: #{key}?" unless !@debug | |
return @redis.hexists(@base,key) | |
end | |
alias_method :include?, :has_key? | |
alias_method :key?, :has_key? | |
alias_method :member?, :has_key? | |
def delete(key) | |
val = unserialize(@redis.hget(@base,key)) | |
@redis.hdel(@base,key) | |
puts "Deleting #{key} with the value: #{val} and returning val" unless !@debug | |
return val | |
end | |
def [](key) | |
val = unserialize(@redis.hget(@base,key)) | |
puts "Fetching value for #{key} with the value: #{val}" unless !@debug | |
return val | |
end | |
def []=(key, value) | |
puts "Setting value: #{value} for key: #{key}" unless !@debug | |
@redis.hset(@base,key,serialize(value)) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment