aboutsummaryrefslogtreecommitdiffstats
path: root/gsxws/core.py
diff options
context:
space:
mode:
authorFilipp Lepalaan <f@230.to>2013-06-01 10:21:35 +0300
committerFilipp Lepalaan <f@230.to>2013-06-01 10:21:35 +0300
commit591a3ae6ec8e7170157bc90ae55eef4ca7f735b0 (patch)
tree037dc788fbbef9d8301c72ea6198420b3a594ba8 /gsxws/core.py
parent7113bab80df32b20979e9e235586f65627a82618 (diff)
downloadpy-gsxws-591a3ae6ec8e7170157bc90ae55eef4ca7f735b0.tar.gz
py-gsxws-591a3ae6ec8e7170157bc90ae55eef4ca7f735b0.tar.bz2
py-gsxws-591a3ae6ec8e7170157bc90ae55eef4ca7f735b0.zip
Use separate cache file for each session
Diffstat (limited to 'gsxws/core.py')
-rw-r--r--gsxws/core.py26
1 files changed, 14 insertions, 12 deletions
diff --git a/gsxws/core.py b/gsxws/core.py
index f586606..a49ea6f 100644
--- a/gsxws/core.py
+++ b/gsxws/core.py
@@ -145,40 +145,42 @@ class GsxError(Exception):
class GsxCache(object):
"""
- >>> GsxCache('spam').set('eggs').get()
+ The cache creates a separate shelf for each GSX session.
+
+ >>> GsxCache('test').set('spam', 'eggs').get('spam')
'eggs'
"""
shelf = None
tmpdir = tempfile.gettempdir()
- filename = os.path.join(tmpdir, "gsxws.tmp")
def __init__(self, key, expires=timedelta(minutes=20)):
self.key = key
self.expires = expires
- self.shelf = shelve.open(self.filename, protocol=-1)
self.now = datetime.now()
+ filename = os.path.join(self.tmpdir, "gsxws_%s.db" % key)
+ self.shelf = shelve.open(filename, protocol=-1)
if not self.shelf.get(key):
# Initialize the key
- self.set(None)
+ self.set(key, None)
- def get(self):
+ def get(self, key):
try:
- d = self.shelf[self.key]
+ d = self.shelf[key]
if d['expires'] > self.now:
return d['value']
else:
- del self.shelf[self.key]
+ del self.shelf[key]
except KeyError:
return None
- def set(self, value):
+ def set(self, key, value):
d = {
'value': value,
'expires': self.now + self.expires
}
- self.shelf[self.key] = d
+ self.shelf[key] = d
return self
@@ -465,14 +467,14 @@ class GsxSession(GsxObject):
def login(self):
global GSX_SESSION
- if not self._cache.get() is None:
- GSX_SESSION = self._cache.get()
+ if not self._cache.get("session") is None:
+ GSX_SESSION = self._cache.get("session")
else:
self._req = GsxRequest(AuthenticateRequest=self)
result = self._req._submit("Authenticate")
self._session_id = result[0].userSessionId
GSX_SESSION = self.get_session()
- self._cache.set(GSX_SESSION)
+ self._cache.set("session", GSX_SESSION)
return GSX_SESSION