comparison ikweb/ikweb/gaeo/session/memcache.py @ 247:7747bbe5b68e

start to develope Information Exchange Center of Ikariam Game. (prototpye)
author "Hisn Yi, Chen <ossug.hychen@gmail.com>"
date Mon, 01 Dec 2008 00:27:22 +0800
parents
children
comparison
equal deleted inserted replaced
246:60c4b4b78a01 247:7747bbe5b68e
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright 2008 Lin-Chieh Shangkuan & Liang-Heng Chen
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 """ GAEO Session - memcache store """
18 import random
19 import pickle
20 import logging
21
22 from google.appengine.api import memcache
23
24 from gaeo import session
25
26 class MemcacheSession(session.Session):
27 """ session that uses memcache """
28
29 def __init__(self, hnd, name = 'gaeo_session', timeout = 60 * 60):
30 super(MemcacheSession, self).__init__(hnd, name, timeout)
31
32 # check from cookie
33 if name in hnd.request.cookies:
34 self._id = hnd.request.cookies[name]
35 session_data = memcache.get(self._id)
36 if session_data:
37 self.update(pickle.loads(session_data))
38 memcache.set(self._id, session_data, timeout)
39 else: # not in the cookie, set it
40 cookie = '%s=%s' % (name, self._id)
41 hnd.response.headers.add_header('Set-Cookie', cookie)
42
43 def put(self):
44 if not self._invalidated:
45 memcache.set(self._id, pickle.dumps(self.copy()), self._timeout)
46
47 def invalidate(self):
48 """Invalidates the session data"""
49 self._hnd.response.headers.add_header(
50 'Set-Cookie',
51 '%s=; expires=Thu, 1-Jan-1970 00:00:00 GMT;' % self._name
52 )
53 memcache.delete(self._id)
54 self.clear()
55 self._invalidated = True