Skip to content

Instantly share code, notes, and snippets.

@rust
Created February 5, 2009 09:33
Show Gist options
  • Save rust/58626 to your computer and use it in GitHub Desktop.
Save rust/58626 to your computer and use it in GitHub Desktop.
class Flare
# limit value size
MAX_VALUE_SIZE = 10_000_000
# connections
@@connection = {
:writer => nil,
:reader => nil
}
@@flare_saver = nil
class << self
# find
def find(klass, id)
hashkey = make_hashkey(klass, id)
setup
value = @@connection[:writer].get(hashkey)
value.blank? ? nil : Marshal.load(value)
end
# save
def save(klass, id, value)
raise "Class is not specified." unless klass
raise "ID is not specified." unless id
dumped_value = Marshal.dump(value)
raise "value exceeds limit size." if dumped_value.size > MAX_VALUE_SIZE
hashkey = make_hashkey(klass, id)
setup
@@connection[:writer].set(hashkey, dumped_value)
end
# setup
def setup
configure
connect
end
private
# configuration
def configure
unless @@flare_saver
@@flare_saver = {
:writer => ["localhost:12121"],
:reader => ["localhost:12121"]
}
end
end
# setup connection
def connect
@@connection
unless @@connection[:writer]
@@connection[:writer] = MemCache.new @@flare_saver[:writer], :namespace => "hogehoge"
end
unless @@connection[:reader]
@@connection[:reader] = MemCache.new @@flare_saver[:reader], :namespace => "hogehoge", :readonly => true
end
end
# make hash-key
def make_hashkey(klass, id)
"#{klass}#{Digest::SHA1.hexdigest(id.to_s)}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment