[Webware-discuss] Memcached Session Submission

2010-03-15 Thread Steve Schwarz
Hi,
I don't recall if I've already submitted this enhancement...
I've written a memcached based Session that others might find useful. Feel
free to use as you wish.
Usage:
1. Name the file SessionMemcachedStore.py in WebKit directory
2. Configure your Application.config:
SessionStore = 'WebKit.SessionMemcachedStore'
SessionMemcachedServers = ['127.0.0.1:11211']
SessionTimeout = 180 # minutes

Best Regards,
Steve

import threading
from memcache import memcache
from WebKit.SessionStore import SessionStore

debug = False


class SessionMemcachedStore(SessionStore, threading.local):
A session memached store.

Stores the sessions in a single memcached using 'last write wins'
semantics.

This is useful for various situations:
1. Fault tolerance
2. Clustering

In clustering configurations with concurrent writes for the same
session(s) the last writer will always overwrite the session.

Cleaning/timing out of sessions is performed by the memcached(s)
themselves
since no one appserver can know about the existence of all sessions or
the last access for a given session. Besides it is built in memcached
functionality. Consequently, correct sizing of memcached(s) is necessary
to hold all user's session data.


## Init ##

def __init__(self, app):
threading.local.__init__(self)
SessionStore.__init__(self, app)

self._memcachedServers = app.setting(
'MemcachedServers', ['127.0.0.1:11211'])

# timeout in seconds
self._sessionTimeout = app.setting(
'SessionTimeout', 180) * 60
## try:
## import cPickle as pickle
## except ImportError:
## import pickle
import pickle
self._client = memcache.Client(self._memcachedServers,

pickleProtocol=pickle.HIGHEST_PROTOCOL)


## Access ##

def __len__(self):
if debug:
print ' len', len(self.keys())
return len(self.keys())

def __getitem__(self, key):
if debug:
print ' getitem(%s)' % (key)
# returns None if key non-existent or no server to contact
value = self._client.get(key)
if value is None:
# SessionStore expects KeyError when no result
raise KeyError
return value

def __setitem__(self, key, item):
if debug:
print ' setitem(%s, %s)' % (key, item)
try:
val = self._client.set(key, item, time=self._sessionTimeout)
if not val:
raise Exception
except Exception, e:
# Not able to store the session is a failure
print Error saving session '%s' to memcached: %s % (key, e)
self.application().handleException()

def __delitem__(self, key):
In contracts with SessionFileStore not finding a key to delete
isn't a KeyError
if debug:
print ' delitem(%s)' % (key)
self._client.delete(key)

def removeKey(self, key):
pass

def has_key(self, key):
if debug:
print  has_key(%s)  % (key)
return self._client.get(key) is not None

def keys(self):
if debug:
print  keys() 
return []

def clear(self):
memcache handles clearing keys itself
if debug:
print  clear() 

def setdefault(self, key, default):
return value associated with key otherwise set key to default and
return default
if debug:
print ' setdefault (%s, %s)' % (key, default)
try:
return self[key]
except KeyError:
self[key] = default
return default


## Application support ##

def storeSession(self, session):
if debug:
print  storeSession(%s)  % (session)
key = session.identifier()
self[key] = session

def storeAllSessions(self):
pass
--
Download Intel#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev___
Webware-discuss mailing list
Webware-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/webware-discuss


Re: [Webware-discuss] Memcached Session Submission

2010-03-15 Thread Christoph Zwerschke
Am 15.03.2010 19:38 schrieb Steve Schwarz:
 I don't recall if I've already submitted this enhancement...
 I've written a memcached based Session that others might find useful.

Thanks, Steve. I'll have a look and probably include it with the next 
Webware release (yes it got postponed but is not abandoned...)

-- Christoph

--
Download Intel#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
___
Webware-discuss mailing list
Webware-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/webware-discuss