From: <nul...@cl...> - 2012-03-28 09:36:30
|
SHIBATA Hiroshi 2012-03-27 15:22:18 +0900 (Tue, 27 Mar 2012) New Revision: 27f667891aed13f5aa5468f3de029618b33b74f4 Merged 97f35d3: Merge pull request #143 from hsbt/memcached Log: imple store_cache/restore_cache Modified files: tdiary/io/heroku.rb Modified: tdiary/io/heroku.rb (+32 -15) =================================================================== --- tdiary/io/heroku.rb 2012-03-27 14:28:12 +0900 (3ae3b3c) +++ tdiary/io/heroku.rb 2012-03-27 15:22:18 +0900 (e157f82) @@ -99,13 +99,12 @@ module TDiary db.transaction do diaries = {} - ymd = date.strftime("%Y%m%d") - - if cache = restore_parser_cache(ymd) + if cache = restore_parser_cache(date) diaries.update(cache) else - restore(ymd, diaries) + restore(date.strftime("%Y%m%d"), diaries) restore_comment(diaries) + clear_cache end dirty = yield(diaries) if iterator? @@ -113,7 +112,7 @@ module TDiary store(diaries) if dirty & TDiary::TDiaryBase::DIRTY_DIARY != 0 store_comment(diaries) if dirty & TDiary::TDiaryBase::DIRTY_COMMENT != 0 - store_parser_cache(ymd, diaries) if dirty || !cache + store_parser_cache(date, diaries) if dirty || !cache end end end @@ -123,21 +122,35 @@ module TDiary end def restore_cache(prefix) - nil + if key = cache_key(prefix) + memcache.get(key) + end end def store_cache(cache, prefix) - nil + if key = cache_key(prefix) + memcache.set(key, cache) + end end - def clear_cache(target = :all) - if target == :all - Dalli::Client.new.flush + def cache_key(prefix) + if @tdiary.is_a?(TDiaryMonth) + "#{prefix}#{@tdiary.rhtml.sub( /month/, @tdiary.date.strftime( '%Y%m' ) ).sub( /\.rhtml$/, '.rb' )}" + elsif @tdiary.is_a?(TDiaryLatest) + if @tdiary.cgi.params['date'][0] + nil + else + "#{prefix}#{@tdiary.rhtml.sub( /\.rhtml$/, '.rb' )}" + end else - Dalli::Client.new.delete(target) + nil end end + def clear_cache(*args) + memcache.flush + end + def calendar calendar = Hash.new{|hash, key| hash[key] = []} Sequel.connect(@tdiary.conf.database_url || ENV['DATABASE_URL']) do |db| @@ -154,12 +167,12 @@ module TDiary private - def restore_parser_cache(ymd) - Dalli::Client.new.get(ymd) + def restore_parser_cache(date) + memcache.get(date.strftime("%Y%m.parser")) end - def store_parser_cache(ymd, obj) - Dalli::Client.new.set(ymd, obj) + def store_parser_cache(date, obj) + memcache.set(date.strftime("%Y%m.parser"), obj) end def restore(date, diaries, month = true) @@ -200,5 +213,9 @@ module TDiary end end end + + def memcache + @_client ||= Dalli::Client.new + end end end |