def stat(bot, update): chat_id = update.message.chat_id user_id = update.message.from_user.id chat_type = update.message.chat.type chat_title = update.message.chat.title last_call = cache.get('last_{}'.format(chat_id)) # First request if not last_call: cache.set('last_{}'.format(chat_id), int(time.time()) - 5) last_call = int(time.time()) - 5 # If last request was over 5 seconds ago if (int(time.time()) - last_call) >= 5: if chat_type == 'group' or chat_type == 'supergroup': # Get stats for group info = Stats.get_chat(chat_id) # Get msg text for /stat msg = Stats.stat_format(chat_id, info['msg_count'], info['current_users'], info['top_users'], chat_title) bot.sendMessage(chat_id, msg, parse_mode=ParseMode.MARKDOWN) # Update last call cache.set('last_{}'.format(chat_id), int(time.time())) logger.info('Group {} requested stats'.format(chat_id))
def get_current_order(user: User, session_id): orders_key = __current_orders_key(user, session_id) orders = cache.get(orders_key) if orders is not None and len(orders) > 0: return orders else: cache.set(orders_key, {}) return {}
def get_current_session(user: User): session_key = __session_key(user) session_id = cache.get(session_key) if is_not_blank(session_id): return Session(session_id, False) else: new_session_id = uuid.uuid4().hex cache.set(session_key, new_session_id) return Session(new_session_id, True)
def get(uid): cached = cache.get('user_{}'.format(uid)) if cached: return cached else: q = db.query(User) \ .filter(User.uid == uid) \ .limit(1) \ .all() if q: cache.set('user_{}'.format(uid), q[0]) cache.delete('web_user_{}'.format(uid)) return q[0] else: return False
def get(cid): cached = cache.get('cstat_{}'.format(cid)) if cached: return cached else: q = db.query(ChatStat) \ .filter(ChatStat.cid == cid) \ .order_by(ChatStat.id.desc()) \ .limit(1) \ .all() if q: cache.set('cstat_{}'.format(cid), q[0]) return q[0] else: return False
def get(uid, cid): cached = cache.get('ustat_{}_{}'.format(uid, cid)) if cached: return cached else: q = db.query(UserStat) \ .filter(UserStat.cid == cid, UserStat.uid == uid) \ .limit(1) \ .all() if q: cache.set('ustat_{}_{}'.format(uid, cid), q[0]) return q[0] else: return False
def get_diskusage(self) -> list: """ 获取磁盘使用情况, 一小时后重新获取 :return: """ # 先从缓存中获取 diskinfo = cache.get('diskinfo') # 获取到了就直接返回 if diskinfo: return diskinfo # 缓存过期后重新获取 disklist = [] disks = psutil.disk_partitions() # 遍历系统磁盘 for disk in disks: # 获取磁盘使用情况 # 磁盘名称 name = disk[0] # 获取磁盘使用信息 try: disk_usage = psutil.disk_usage(name) except: continue # 获取磁盘总额 total = disk_usage.total / (1024 * 1024 * 1024) # 获取磁盘已用空间 used = disk_usage.used / (1024 * 1024 * 1024) # 获取磁盘可用空间 free = disk_usage.free / (1024 * 1024 * 1024) # 获取磁盘使用百分比 percent = disk_usage.percent # 添加到列表 if "Windows" in self.__system_info["os"]: name = name[:-2] else: name = name.replace('/', '') disklist.append({ "name": name, "total": total, "used": used, "free": free, "percent": percent }) # 设置缓存设置过期时间为1小时 cache.set('diskinfo', disklist, timeout=60 * 60 * 60) return disklist