Hatena::Grouprubyist

yuisekiのいまさらruby厨日記

2010-10-02

PStoreをJSONで

15:06 | はてなブックマーク - PStoreをJSONで - yuisekiのいまさらruby厨日記

いろんなサービスのAPI結果などをキャッシュする的な用途を想定

require 'rubygems'
require 'sinatra'
require 'json/pure'
require 'pstore'
require 'active_support'
class JsonStore < PStore
  def initialize(file); super(file); end
  def dump(table); table.to_json; end
  def load(content); JSON.load(content); end
end
db = JsonStore.new('json.dat')
helpers do
  def db_init(db, service, user)
    db[service] = Hash.new unless db[service]
    db[service][user] = Hash.new unless db[service][user]
    db[service][user]["updated_at"] = Time.now.to_i-3600 unless db[service][user]["updated_at"]
    return db[service][user]
  end
end
トラックバック - http://rubyist.g.hatena.ne.jp/yuiseki/20101002