Created
March 22, 2009 21:29
-
-
Save masuidrive/83303 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
-- | |
-- expire record and memcached record | |
-- | |
require('Memcached') -- http://luamemcached.luaforge.net/ | |
-- http://mixi.jp/view_bbs.pl?id=33263045&comm_id=3528783 #20 | |
connect_memcache() | |
function expire() | |
local args = {} | |
local cdate = string.format("%d", _time()) | |
table.insert(args, "addcond\0exptime\0NUMLE\0" .. cdate) | |
local res = _misc("search", args) | |
if res then | |
for i = 1, #res do | |
local row = _get(res[i]) | |
local user_id = get_rdb_value(_get(res[i]), 'user_id') | |
if user_id then | |
expire_user_cache(user_id) | |
end | |
_out(res[i]) | |
end | |
else | |
_log("expiration was failed") | |
end | |
end | |
function get_rdb_value(row, key) | |
local escaped_key = key:gsub("%%","%%%%") | |
local s,e,result = string.find("\000"..row.."\000", "%z"..escaped_key.."%z([^%z]*)%z") | |
return(result) | |
end | |
function expire_user_cache(user_id) | |
if memcache == nil then | |
connect_memcache() | |
end | |
_log("expire user cache: " .. user_id, 0) | |
if not pcall(function(user_id) | |
memcache:delete("mailfeeds:user_feed:" .. user_id) | |
-- and more... | |
end, user_id) then | |
memcache = nil | |
end | |
end | |
function connect_memcache() | |
if pcall(function() | |
memcache = Memcached.Connect('localhost', 11211) | |
end) then | |
_log("connecet_memcache: Success") | |
else | |
memcache = nil | |
_log("connecet_memcache: Failure") | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment