def update(self, **kwargs): # means that the session has been found and needs an update if self.op == 'eq' and self.field == 'id' and self.value: key = self.keyprefix + ':' + str(self.value) if not self.db.r_server.exists(key): return None with self.db.r_server.pipeline() as pipe: pipe.hmset(key, kwargs) if self.session_expiry: pipe.expire(key, self.session_expiry) rtn = pipe.execute()[0] if self.with_lock: release_lock(self.db, key + ':lock', self.value) return rtn
def __call__(self, key, f, time_expire=300, with_lock=None): if with_lock is None: with_lock = self.with_lock if time_expire is None: time_expire = 24 * 60 * 60 newKey = self.__keyFormat__(key) value = None ttl = 0 try: if f is None: # delete and never look back self.r_server.delete(newKey) return None # is there a value obj = self.r_server.get(newKey) # what's its ttl if obj: ttl = self.r_server.ttl(newKey) if ttl > time_expire: obj = None if obj: # was cached if self.debug: self.r_server.incr('web2py_cache_statistics:hit_total') value = pickle.loads(obj) else: # naive distributed locking if with_lock: lock_key = '%s:__lock' % newKey randomvalue = time.time() al = acquire_lock(self.r_server, lock_key, randomvalue) try: # someone may have computed it obj = self.r_server.get(newKey) if obj is None: value = self.cache_it(newKey, f, time_expire) else: value = pickle.loads(obj) finally: release_lock(self, lock_key, al) else: # without distributed locking value = self.cache_it(newKey, f, time_expire) return value except RConnectionError: return self.retry_call(key, f, time_expire, with_lock)
def insert(self, **kwargs): # usually kwargs would be a Storage with several keys: # 'locked', 'client_ip','created_datetime','modified_datetime' # 'unique_key', 'session_data' # retrieve a new key newid = str(self.db.r_server.incr(self.serial)) key = self.keyprefix + ':' + newid if self.with_lock: key_lock = key + ':lock' acquire_lock(self.db.r_server, key_lock, newid) with self.db.r_server.pipeline() as pipe: # add it to the index pipe.sadd(self.id_idx, key) # set a hash key with the Storage pipe.hmset(key, kwargs) if self.session_expiry: pipe.expire(key, self.session_expiry) pipe.execute() if self.with_lock: release_lock(self.db, key_lock, newid) return newid