This is a semi-quick key/value store I put together for a quick way to store temporary data related to what's in my database, in a reliable way. For example, I'm using it to keep track of where a hourly and a daily crontask that processes statistics left off, so it can resume only processing and summarizing new data on the next run.
Code quality most likely is not great, but it works. And I will update this gist as I update my project.
The settings table has two columns, a key
and a value
column. The value
column is serialized, so you can technically store almost anything in there. Memcache is also used as a caching layer to minimize database calls.
# fetching a value
>> Setting.hello
=> nil
# setting a value, writes it to memcache, and saves or updates a database
# record is the form of {:key => "hello", :value => serialize(["world"])}
>> Setting.hello = ["world"]
=> ["hello"]
# fetching the value again, which will only read from memcache
>> Setting.hello
=> ["hello"]
# force fetch from database and update memcache
>> Setting.hello!
=> ["hello"]
# remove the hello key from memcache and the database
>> Setting.hello = nil
=> nil
# default memcache expire_in option is 6.hours, you can change it...
>> Setting.__cache_expires_in = 1.hour
=> 3600 seconds
Very nice, thank you for sharing.