def CalcDataGram(n, m, reverse=True): n = min(n, 10) m = min(m, 10) mc = MemcacheConn().getMcConn('wp') mcKey = 'wp-datagram-%s-%s-%s' % (n, m, reverse) mcRet = mc.get(mcKey) if mcRet is not None: return json.loads(mcRet) ret = {} i = RuleEngine.TRY_TIMES totalSum = 0 while i > 0: i -= 1 diceList = RuleEngine.RollDice(n, m, reverse) sum = 0 for dice in diceList: sum += dice totalSum += sum if ret.has_key('%s' % sum): ret['%s' % sum] += 1 else: ret['%s' % sum] = 1 _ret = {} for k in ret.keys(): _ret[int(k)] = ret[k] _sortedRet = [] for k in sorted(_ret.keys()): _sortedRet.append({"k": k, "v": _ret[k]}) ret = { 'exp': float('%0.3f' % (float(totalSum) / float(RuleEngine.TRY_TIMES))), 'list': _sortedRet } mc.set(mcKey, json.dumps(ret), 3600) return ret
def getDiceHistory(): mc = MemcacheConn().getMcConn('wp') mcRet = mc.get('wp-dice-history') if mcRet is None: diceHistory = [] else: diceHistory = json.loads(mcRet) return {'status': 'succeed', 'data': diceHistory}
def addDiceHistory(msg): try: mc = MemcacheConn().getMcConn('wp') mcRet = mc.get('wp-dice-history') if mcRet is None: diceHistory = [] else: diceHistory = json.loads(mcRet) diceHistory.append({'ctime': time.time(), 'msg': msg}) if len(diceHistory) > HistoryClient.MAX_LIST_SIZE: del diceHistory[0] mc.set('wp-dice-history', json.dumps(diceHistory)) return {'status': 'succeed', 'data': diceHistory} except: LogClient.error(sys.exc_info()) return {'status': 'succeed', 'data': []}
def CalcExp(n, m, reverse=True): n = min(n, 10) m = min(m, 10) mc = MemcacheConn().getMcConn('wp') mcKey = 'wp-exp-%s-%s-%s' % (n, m, reverse) mcRet = mc.get(mcKey) if mcRet is not None: return mcRet totalSum = 0 i = RuleEngine.TRY_TIMES while i > 0: i -= 1 diceList = RuleEngine.RollDice(n, m, reverse) sum = 0 for dice in diceList: sum += dice totalSum += sum ret = float('%0.3f' % (float(totalSum) / float(RuleEngine.TRY_TIMES))) mc.set(mcKey, ret, 3600) return ret
class MemcacheStore(Store): def __init__(self, timeout = 604800): self.mc = MemcacheConn().getMcConn('sessions') self.timeout = timeout def __contains__(self, key): return True if self.mc.get(key) else False def __getitem__(self, key): return self.mc.get(key) def __setitem__(self, key, value): self.mc.set(key, value, self.timeout) def __delitem__(self, key): self.mc.delete(key) def cleanup(self, timeout): '''You need nothing to do. Memcache can handle it.''' pass
def __init__(self, timeout = 604800): self.mc = MemcacheConn().getMcConn('sessions') self.timeout = timeout
def __test(self): from common.dao import MemcacheConn mc = MemcacheConn() # mc.getMcConn('wp').set('test', 'helloworld!') LogClient.info(mc.getMcConn('wp').get('test'))