29 lines
713 B
Python
29 lines
713 B
Python
# encoding:utf-8
|
|
import time
|
|
|
|
|
|
class WechatToken(object):
|
|
# Singleton
|
|
_instance = None
|
|
_cache = {}
|
|
|
|
def __new__(cls, *args, **kw):
|
|
"""单例模式"""
|
|
if not cls._instance:
|
|
cls._instance = super(WechatToken, cls).__new__(cls, *args, **kw)
|
|
return cls._instance
|
|
|
|
def get_token(self, appid):
|
|
data = self._cache.get(appid)
|
|
if data is None:
|
|
return None
|
|
now = round(time.time())
|
|
if data[1] - now < 1200:
|
|
return None
|
|
return data[0]
|
|
|
|
def update_token(self, appid, token, exptime):
|
|
now = round(time.time()) + exptime
|
|
data = (token, now)
|
|
self._cache[appid] = data
|