lib/ramaze/cache/memcache.rb in Pistos-ramaze-2009.04.08 vs lib/ramaze/cache/memcache.rb in Pistos-ramaze-2009.06.12
- old
+ new
@@ -11,11 +11,24 @@
#
# Please read the documentation of memcache-client for further methods.
#
# It is highly recommended to install memcache-client_extensions for
# a bit of speedup and more functionality
+ #
+ # NOTE: There is a big issue with persisting sessions in memcache, not only
+ # can they be dropped at any time, essentially logging the user out
+ # without them noticing, but there is also a low limit to the maximum
+ # time-to-live. After 30 days, your session will be dropped, no
+ # matter what.
+ # Please remember that memcache is, first of all, a cache, not a
+ # persistence mechanism.
+ #
+ # NOTE: If you try to set a higher ttl than allowed, your stored key/value
+ # will be expired immediately.
class MemCache
+ MAX_TTL = 2592000
+
include Cache::API
# +:multithread+: May be turned off at your own risk.
# +:readonly+: You most likely want that to be false.
# +:servers+: Array containing at least one of:
@@ -32,10 +45,11 @@
def cache_setup(host, user, app, name)
@namespace = [host, user, app, name].compact.join('-')
options = {:namespace => @namespace}.merge(OPTIONS)
servers = options.delete(:servers)
@store = ::MemCache.new(servers, options)
+ @warned = false
end
# Wipe out _all_ data in memcached, use with care.
def cache_clear
@store.flush_all
@@ -44,11 +58,11 @@
nil
end
#
def cache_delete(*keys)
- super{|key| @store.delete(key) }
+ super{|key| @store.delete(key); nil }
rescue ::MemCache::MemCacheError => e
Log.error(e)
nil
end
@@ -63,9 +77,20 @@
nil
end
def cache_store(key, value, options = {})
ttl = options[:ttl] || 0
+
+ if ttl > MAX_TTL
+ unless @warned
+ Log.warn('MemCache cannot set a ttl greater than 2592000 seconds.')
+ Log.warn('Modify Ramaze.options.session.ttl to a value <= of that.')
+ @warned = true
+ end
+
+ ttl = MAX_TTL
+ end
+
@store.set(key, value, ttl)
value
rescue ::MemCache::MemCacheError => e
Log.error(e)
nil