def _reset(self): RedisDB.connect() MongoDB.connect() GiftCodeUsingLog.objects.all().delete() # GiftCodeRecord.objects.all().delete() # GiftCodeGen.objects.all().delete() # GiftCode.objects.all().delete() MailHistoryRecord.objects.all().delete() Statistics.objects.all().delete() Character.objects.all().delete() ForbidChat.objects.all().delete() AccountBan.objects.all().delete() AccountLoginLog.objects.all().delete() AccountThird.objects.all().delete() AccountRegular.objects.all().delete() Account.objects.all().delete() GeTuiClientID.objects.all().delete() RedisDB.get().flushall() for mc in MongoDB.DBS.values(): mc.client.drop_database(mc.name) for s in Server.objects.all(): ensure_index(s.id)
def new(cls, account_id, value=None): key = LoginID.make_key(account_id) if not value: value = make_short_random_string() RedisDB.get(1).setex(key, value, cls.ID_EXPIRE) return value
def lock(self, wait_timeout=5, hold_seconds=5): """ :param wait_timeout: 等待获取锁的超时时间,超过这个时间没有获取到锁,将抛出LockTimeOut异常 :param hold_seconds: 获取到锁以后,要保持的最长时间。 程序在可以在这个时间点前可以主动放弃锁。 如果超过hold_seconds后,还没有主动放弃,那么锁将自动释放 以此来应对程序获得锁以后出错没有主动释放形成死锁的问题 """ t = 0 while True: if t > wait_timeout: raise LockTimeOut() result = RedisDB.get().set(self.key, 1, ex=hold_seconds, nx=True) if result: # got the lock break time.sleep(self.INTERVAL) t += self.INTERVAL try: yield self finally: RedisDB.get().delete(self.key)
def set_forbidden(self, expire_at): key = make_forbidden_key(self.char_id) expire = expire_at - arrow.utcnow().timestamp if expire <= 0: return RedisDB.get(2).setex(key, 1, expire)
def get(self): with RedisDB.get().pipeline() as p: p.lrange(self.key, 0, -1) p.delete(self.key) result = p.execute() return result[0]
def set(self, cd_seconds): key = self.make_key() result = RedisDB.get().set(key, 1, ex=cd_seconds, nx=True) if not result: # 是设置失败,cd还没有消失 return False return True
def put(self, msg=None, data=None): # data is Serialized msg if msg is not None: data = MessageFactory.pack(msg) assert data is not None with RedisDB.get().pipeline() as p: p.rpush(self.key, data) p.expire(self.key, EXPIRE) p.execute()
def release(self): RedisDB.get().delete(self.key)
def delete(key): RedisDB.get().delete(key)
def remove_lock_key(key): RedisDB.get().delete(key)
def clean(self): RedisDB.get().delete(self.make_key())
def set(key, obj, expire=CACHE_SECONDS): data = cPickle.dumps(obj, HIGHEST_PROTOCOL) if expire: RedisDB.get().setex(key, data, expire) else: RedisDB.get().set(key, data)
def teardown(): from core.db import MongoDB, RedisDB mongo = MongoDB.get(1) mongo.client.drop_database(mongo.name) RedisDB.get().flushdb()
def clean(self): RedisDB.get().delete(self.key)
def update_expire(cls, account_id): key = LoginID.make_key(account_id) RedisDB.get(1).expire(key, cls.ID_EXPIRE)
def delete(cls, account_id): key = LoginID.make_key(account_id) RedisDB.get(1).delete(key)
def get(cls, account_id,): key = LoginID.make_key(account_id) return RedisDB.get(1).get(key)
def flush(): RedisDB.get().flushdb()
def remove_forbidden(self): key = make_forbidden_key(self.char_id) RedisDB.get(2).delete(key)
def get_cd_seconds(self): ttl = RedisDB.get().ttl(self.make_key()) if not ttl: return 0 return ttl
def _empty_cache(self): RedisDB.connect() RedisDB.get().flushall()
def relogin(request): RedisDB.get(1).flushdb() return HttpResponse("Success!")
def check_forbidden(self): key = make_forbidden_key(self.char_id) if RedisDB.get(2).get(key): raise GameException(ConfigErrorMessage.get_error_id("CHAT_FORBIDDEN"))
def get(key): data = RedisDB.get().get(key) if data: return cPickle.loads(data) return None
def get( cls, account_id, ): key = LoginID.make_key(account_id) return RedisDB.get(1).get(key)
def __call__(self, request): RedisDB.connect() MongoDB.connect() return self.get_response(request)