provide new memcached-based session handler
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@4101 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
1f96d0919a
commit
463b6526b3
3 changed files with 149 additions and 0 deletions
3
session/__init__.py
Normal file
3
session/__init__.py
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
"""
|
||||||
|
$Id$
|
||||||
|
"""
|
102
session/memcached.py
Normal file
102
session/memcached.py
Normal file
|
@ -0,0 +1,102 @@
|
||||||
|
#
|
||||||
|
# Copyright (c) 2010 Helmut Merz helmutm@cy55.de
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation; either version 2 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program; if not, write to the Free Software
|
||||||
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
#
|
||||||
|
|
||||||
|
"""
|
||||||
|
Store session data in memcached.
|
||||||
|
|
||||||
|
$Id$
|
||||||
|
"""
|
||||||
|
|
||||||
|
from zope.app.session.interfaces import IClientId, ISession
|
||||||
|
from zope.app.session.interfaces import ISessionDataContainer
|
||||||
|
from zope.app.session.interfaces import ISessionPkgData, ISessionData
|
||||||
|
from zope import component
|
||||||
|
from zope.component import getUtility, adapts
|
||||||
|
from zope.component.interfaces import ComponentLookupError
|
||||||
|
from zope.interface import implements
|
||||||
|
from zope.publisher.interfaces import IRequest
|
||||||
|
from lovely.memcached.interfaces import IMemcachedClient
|
||||||
|
|
||||||
|
|
||||||
|
class SessionDataContainer(object):
|
||||||
|
|
||||||
|
implements(ISessionDataContainer)
|
||||||
|
|
||||||
|
lifetime = 8 * 3600
|
||||||
|
namespace = 'cybertools.session'
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
client = component.getUtility(IMemcachedClient)
|
||||||
|
return client.query(key, ns=self.namespace)
|
||||||
|
|
||||||
|
def __setitem__(self, key, value):
|
||||||
|
client = component.getUtility(IMemcachedClient)
|
||||||
|
#print '***', key, value
|
||||||
|
client.set(value, key, lifetime=self.lifetime, ns=self.namespace)
|
||||||
|
|
||||||
|
|
||||||
|
class Session(object):
|
||||||
|
|
||||||
|
implements(ISession)
|
||||||
|
adapts(IRequest)
|
||||||
|
|
||||||
|
packageName = 'cybertools.session.memcached'
|
||||||
|
|
||||||
|
def __init__(self, request):
|
||||||
|
self.client_id = str(IClientId(request))
|
||||||
|
|
||||||
|
def __getitem__(self, pkg_id):
|
||||||
|
sdc = getUtility(ISessionDataContainer, name=self.packageName)
|
||||||
|
sd = sdc[self.client_id]
|
||||||
|
if sd is None:
|
||||||
|
sd = sdc[self.client_id] = SessionData(self.client_id, sdc)
|
||||||
|
try:
|
||||||
|
return sd[pkg_id]
|
||||||
|
except KeyError:
|
||||||
|
spd = sd[pkg_id] = SessionPkgData(pkg_id, sd)
|
||||||
|
return spd
|
||||||
|
|
||||||
|
|
||||||
|
class SessionData(dict):
|
||||||
|
|
||||||
|
implements(ISessionData)
|
||||||
|
|
||||||
|
def __init__(self, id, parent):
|
||||||
|
self.id = id
|
||||||
|
self.parent = parent
|
||||||
|
|
||||||
|
def __setitem__(self, key, value):
|
||||||
|
super(SessionData, self).__setitem__(key, value)
|
||||||
|
self.parent[self.id] = self
|
||||||
|
|
||||||
|
def setdefault(self, key, default):
|
||||||
|
try:
|
||||||
|
return self[key]
|
||||||
|
except KeyError:
|
||||||
|
self[key] = default
|
||||||
|
return default
|
||||||
|
|
||||||
|
def clear(self):
|
||||||
|
super(SessionData, self).clear()
|
||||||
|
self.parent[self.id] = self
|
||||||
|
|
||||||
|
|
||||||
|
class SessionPkgData(SessionData):
|
||||||
|
|
||||||
|
implements(ISessionPkgData)
|
||||||
|
|
44
session/memcached.zcml
Normal file
44
session/memcached.zcml
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
<!-- $Id$ -->
|
||||||
|
|
||||||
|
<configure
|
||||||
|
xmlns="http://namespaces.zope.org/zope"
|
||||||
|
xmlns:browser="http://namespaces.zope.org/browser"
|
||||||
|
i18n_domain="cybertools.session">
|
||||||
|
|
||||||
|
<adapter
|
||||||
|
factory="cybertools.session.memcached.Session"
|
||||||
|
provides="zope.app.session.interfaces.ISession"
|
||||||
|
permission="zope.Public" />
|
||||||
|
|
||||||
|
<!--<adapter
|
||||||
|
factory="cybertools.session.memcached.Session"
|
||||||
|
provides="zope.traversing.interfaces.IPathAdapter"
|
||||||
|
name="session" />-->
|
||||||
|
|
||||||
|
<utility
|
||||||
|
factory="cybertools.session.memcached.SessionDataContainer"
|
||||||
|
name="cybertools.session.memcached" />
|
||||||
|
|
||||||
|
<class class="cybertools.session.memcached.Session">
|
||||||
|
<allow interface="zope.app.session.interfaces.ISession" />
|
||||||
|
<implements interface="zope.traversing.interfaces.IPathAdapter" />
|
||||||
|
</class>
|
||||||
|
|
||||||
|
<class class="cybertools.session.memcached.SessionDataContainer">
|
||||||
|
<require
|
||||||
|
interface="zope.app.session.interfaces.ISessionDataContainer"
|
||||||
|
permission="zope.Public" />
|
||||||
|
<require
|
||||||
|
set_schema="zope.app.session.interfaces.ISessionDataContainer"
|
||||||
|
permission="zope.ManageServices" />
|
||||||
|
</class>
|
||||||
|
|
||||||
|
<class class="cybertools.session.memcached.SessionData">
|
||||||
|
<allow interface="zope.app.session.interfaces.ISessionData" />
|
||||||
|
</class>
|
||||||
|
|
||||||
|
<class class="cybertools.session.memcached.SessionPkgData">
|
||||||
|
<allow interface="zope.app.session.interfaces.ISessionPkgData" />
|
||||||
|
</class>
|
||||||
|
|
||||||
|
</configure>
|
Loading…
Add table
Reference in a new issue