Created
December 18, 2011 11:11
-
-
Save flexd/1493046 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.to_s | |
@debug = true | |
load_data | |
# 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 | |
if object == nil then return {} | |
puts "The fetched object was nil!!!" | |
else | |
puts "We should have a good object!" | |
return JSON.parse(object) | |
end | |
end | |
def [](key) | |
@data[key] | |
end | |
def []=(key, value) | |
puts "Setting #{key} to: #{value}" unless !@debug | |
@data[key] = value | |
maybe_save | |
end | |
def has_key?(key) | |
@data.has_key?(key) | |
end | |
alias_method :include?, :has_key? | |
alias_method :key?, :has_key? | |
alias_method :member?, :has_key? | |
def each | |
@data.each {|e| yield(e)} | |
end | |
def each_key | |
@data.each_key {|e| yield(e)} | |
end | |
def each_value | |
@data.each_value {|e| yield(e)} | |
end | |
def delete(key) | |
@data.delete(key) | |
maybe_save | |
end | |
def delete_if | |
delete_keys = [] | |
each do |key, value| | |
delete_keys << key if yield(key, value) | |
end | |
delete_keys.each do |key| | |
delete(key) | |
end | |
end | |
def save | |
puts "I AM SAVING THIS MOFO!!!!22222" | |
result = @redis.set(@base, serialize(@data)) | |
puts result.inspect | |
end | |
def load_data | |
@data = unserialize(@redis.get(@base)) | |
puts "Data is now: #{@data}" | |
end | |
def unload | |
end | |
private | |
def maybe_save | |
save if @options.autosave | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment