本文整理汇总了Python中werkzeug.contrib.cache.FileSystemCache.delete方法的典型用法代码示例。如果您正苦于以下问题:Python FileSystemCache.delete方法的具体用法?Python FileSystemCache.delete怎么用?Python FileSystemCache.delete使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类werkzeug.contrib.cache.FileSystemCache
的用法示例。
在下文中一共展示了FileSystemCache.delete方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: FileSystemSessionInterface
# 需要导入模块: from werkzeug.contrib.cache import FileSystemCache [as 别名]
# 或者: from werkzeug.contrib.cache.FileSystemCache import delete [as 别名]
class FileSystemSessionInterface(SessionInterface):
"""Uses the :class:`werkzeug.contrib.cache.FileSystemCache` as a session
backend.
:param cache_dir: the directory where session files are stored.
:param threshold: the maximum number of items the session stores before it
starts deleting some.
:param mode: the file mode wanted for the session files, default 0600
:param key_prefix: A prefix that is added to FileSystemCache store keys.
"""
session_class = FileSystemSession
def __init__(self, cache_dir, threshold, mode, key_prefix):
from werkzeug.contrib.cache import FileSystemCache
self.cache = FileSystemCache(cache_dir, threshold=threshold, mode=mode)
self.key_prefix = key_prefix
def _generate_sid(self):
return str(uuid4())
def open_session(self, app, request):
sid = request.cookies.get(app.session_cookie_name)
if not sid:
sid = self._generate_sid()
return self.session_class(sid=sid)
data = self.cache.get(self.key_prefix + sid)
if data is not None:
return self.session_class(data, sid=sid)
return self.session_class(sid=sid)
def save_session(self, app, session, response):
domain = self.get_cookie_domain(app)
path = self.get_cookie_path(app)
if not session:
if session.modified:
self.cache.delete(self.key_prefix + session.sid)
response.delete_cookie(app.session_cookie_name,
domain=domain, path=path)
return
# Modification case. There are upsides and downsides to
# emitting a set-cookie header each request. The behavior
# is controlled by the :meth:`should_set_cookie` method
# which performs a quick check to figure out if the cookie
# should be set or not. This is controlled by the
# SESSION_REFRESH_EACH_REQUEST config flag as well as
# the permanent flag on the session itself.
# if not self.should_set_cookie(app, session):
# return
httponly = self.get_cookie_httponly(app)
secure = self.get_cookie_secure(app)
expires = self.get_expiration_time(app, session)
data = dict(session)
self.cache.set(self.key_prefix + session.sid, data,
int(app.permanent_session_lifetime.total_seconds()))
response.set_cookie(app.session_cookie_name, session.sid,
expires=expires, httponly=httponly,
domain=domain, path=path, secure=secure)
示例2: WechatCache
# 需要导入模块: from werkzeug.contrib.cache import FileSystemCache [as 别名]
# 或者: from werkzeug.contrib.cache.FileSystemCache import delete [as 别名]
class WechatCache(object):
"""基于文件的缓存
"""
def __init__(self, cache_dir='cache', default_timeout=300):
"""初始化
cache_dir是缓存目录
"""
self.cache = FileSystemCache(cache_dir, default_timeout=default_timeout)
def clear(self):
"""清空缓存
"""
return self.cache.clear()
def get(self, key):
"""获取缓存
获取键值key的缓存值
如果没有对应缓存,返回None
"""
return self.cache.get(key)
def add(self, key, value, timeout=None):
"""增加缓存
如果键值key对应的缓存不存在,那么增加值value到键值key,过期时间timeout,默认300秒
否则返回False(即不能覆盖设置缓存)
"""
return self.cache.add(key, value, timeout)
def set(self, key, value, timeout=None):
"""设置缓存
设置键值key的缓存为value,过期时间300秒
"""
return self.cache.set(key, value, timeout)
def delete(self, key):
"""删除缓存
删除键值key存储的缓存
"""
return self.cache.delete(key)
示例3: FileSystemSessionInterface
# 需要导入模块: from werkzeug.contrib.cache import FileSystemCache [as 别名]
# 或者: from werkzeug.contrib.cache.FileSystemCache import delete [as 别名]
class FileSystemSessionInterface(SessionInterface):
"""Uses the :class:`werkzeug.contrib.cache.FileSystemCache` as a session
backend.
.. versionadded:: 0.2
The `use_signer` parameter was added.
:param cache_dir: the directory where session files are stored.
:param threshold: the maximum number of items the session stores before it
starts deleting some.
:param mode: the file mode wanted for the session files, default 0600
:param key_prefix: A prefix that is added to FileSystemCache store keys.
:param use_signer: Whether to sign the session id cookie or not.
:param permanent: Whether to use permanent session or not.
"""
session_class = FileSystemSession
def __init__(self, cache_dir, threshold, mode, key_prefix,
use_signer=False, permanent=True):
from werkzeug.contrib.cache import FileSystemCache
self.cache = FileSystemCache(cache_dir, threshold=threshold, mode=mode)
self.key_prefix = key_prefix
self.use_signer = use_signer
self.permanent = permanent
def open_session(self, app, request):
sid = request.cookies.get(app.session_cookie_name)
if not sid:
sid = self._generate_sid()
return self.session_class(sid=sid, permanent=self.permanent)
if self.use_signer:
signer = self._get_signer(app)
if signer is None:
return None
try:
sid = signer.unsign(sid)
except BadSignature:
sid = self._generate_sid()
return self.session_class(sid=sid, permanent=self.permanent)
data = self.cache.get(self.key_prefix + sid)
if data is not None:
return self.session_class(data, sid=sid)
return self.session_class(sid=sid, permanent=self.permanent)
def save_session(self, app, session, response):
domain = self.get_cookie_domain(app)
path = self.get_cookie_path(app)
if not session:
if session.modified:
self.cache.delete(self.key_prefix + session.sid)
response.delete_cookie(app.session_cookie_name,
domain=domain, path=path)
return
httponly = self.get_cookie_httponly(app)
secure = self.get_cookie_secure(app)
expires = self.get_expiration_time(app, session)
data = dict(session)
self.cache.set(self.key_prefix + session.sid, data,
total_seconds(app.permanent_session_lifetime))
if self.use_signer:
session_id = self._get_signer(app).sign(session.sid)
else:
session_id = session.sid
response.set_cookie(app.session_cookie_name, session_id,
expires=expires, httponly=httponly,
domain=domain, path=path, secure=secure)
示例4: Cache
# 需要导入模块: from werkzeug.contrib.cache import FileSystemCache [as 别名]
# 或者: from werkzeug.contrib.cache.FileSystemCache import delete [as 别名]
class Cache(object):
"""
This class is used to control the cache objects.
If TESTING is True it will use NullCache.
"""
def __init__(self, app=None):
self.cache = None
if app is not None:
self.init_app(app)
else:
self.app = None
def init_app(self, app):
"This is used to initialize cache with your app object"
app.config.setdefault('CACHE_DEFAULT_TIMEOUT', 300)
app.config.setdefault('CACHE_THRESHOLD', 500)
app.config.setdefault('CACHE_KEY_PREFIX', None)
app.config.setdefault('CACHE_MEMCACHED_SERVERS', None)
app.config.setdefault('CACHE_DIR', None)
app.config.setdefault('CACHE_TYPE', 'NullCache')
self.app = app
self._set_cache()
def _set_cache(self):
if self.app.config['TESTING']:
self.cache = NullCache()
else:
if self.app.config['CACHE_TYPE'] == 'Null':
self.cache = NullCache()
elif self.app.config['CACHE_TYPE'] == 'Simple':
self.cache = SimpleCache(
threshold=self.app.config['CACHE_THRESHOLD'],
default_timeout=self.app.config['CACHE_DEFAULT_TIMEOUT'])
elif self.app.config['CACHE_TYPE'] == 'Memcached':
self.cache = MemcachedCache(
self.app.config['CACHE_MEMCACHED_SERVERS'],
default_timeout=self.app.config['CACHE_DEFAULT_TIMEOUT'],
key_prefix=self.app.config['CACHE_KEY_PREFIX'])
elif self.app.config['CACHE_TYPE'] == 'GAE':
self.cache = GAEMemcachedCache(
default_timeout=self.app.config['CACHE_DEFAULT_TIMEOUT'],
key_prefix=self.app.config['CACHE_KEY_PREFIX'])
elif self.app.config['CACHE_TYPE'] == 'FileSystem':
self.cache = FileSystemCache(
self.app.config['CACHE_DIR'],
threshold=self.app.config['CACHE_THRESHOLD'],
default_timeout=self.app.config['CACHE_DEFAULT_TIMEOUT'])
def get(self, *args, **kwargs):
"Proxy function for internal cache object."
return self.cache.get(*args, **kwargs)
def set(self, *args, **kwargs):
"Proxy function for internal cache object."
self.cache.set(*args, **kwargs)
def add(self, *args, **kwargs):
"Proxy function for internal cache object."
self.cache.add(*args, **kwargs)
def delete(self, *args, **kwargs):
"Proxy function for internal cache object."
self.cache.delete(*args, **kwargs)
def cached(self, timeout=None, key_prefix='view/%s', unless=None):
"""
Decorator. Use this to cache a function. By default the cache key
is `view/request.path`. You are able to use this decorator with any
function by changing the `key_prefix`. If the token `%s` is located
within the `key_prefix` then it will replace that with `request.path`
Example::
# An example view function
@cache.cached(timeout=50)
def big_foo():
return big_bar_calc()
# An example misc function to cache.
@cache.cached(key_prefix='MyCachedList')
def get_list():
return [random.randrange(0, 1) for i in range(50000)]
.. code-block:: pycon
>>> my_list = get_list()
:param timeout: Default None. If set to an integer, will cache for that
amount of time.
:param key_prefix: Default 'view/%(request.path)s'. Beginning key to .
use for the cache key.
:param unless: Default None. Cache will *always* execute the caching
facilities unless this callable is true.
This will bypass the caching entirely.
#.........这里部分代码省略.........
示例5: FileSystemSessionInterface
# 需要导入模块: from werkzeug.contrib.cache import FileSystemCache [as 别名]
# 或者: from werkzeug.contrib.cache.FileSystemCache import delete [as 别名]
class FileSystemSessionInterface(SessionInterface):
"""Uses the :class:`werkzeug.contrib.cache.FileSystemCache` as a session
backend.
.. versionadded:: 0.2
The `use_signer` parameter was added.
:param cache_dir: the directory where session files are stored.
:param threshold: the maximum number of items the session stores before it
starts deleting some.
:param mode: the file mode wanted for the session files, default 0600
:param key_prefix: A prefix that is added to FileSystemCache store keys.
:param use_signer: Whether to sign the session id cookie or not.
"""
session_class = FileSystemSession
def __init__(self, cache_dir, threshold, mode, key_prefix,
use_signer=False):
from werkzeug.contrib.cache import FileSystemCache
self.cache = FileSystemCache(cache_dir, threshold=threshold, mode=mode) #FileSystemCache是从上句导入的,threshold代表最多纪录条数
self.key_prefix = key_prefix
self.use_signer = use_signer
def open_session(self, app, request):
sid = request.cookies.get(app.session_cookie_name)
if not sid:
sid = self._generate_sid()
return self.session_class(sid=sid)
if self.use_signer:
signer = self._get_signer(app)
if signer is None:
return None
try:
sid = signer.unsign(sid)
except BadSignature:
sid = None
data = self.cache.get(self.key_prefix + sid) #这是根据session_id获取session值
if data is not None:
return self.session_class(data, sid=sid)
return self.session_class(sid=sid)
def save_session(self, app, session, response):
domain = self.get_cookie_domain(app)
path = self.get_cookie_path(app)
if not session: #验证过了,就是判断session是否为空(空字典)
if session.modified:
self.cache.delete(self.key_prefix + session.sid)
response.delete_cookie(app.session_cookie_name,
domain=domain, path=path)
return
# Modification case. There are upsides and downsides to
# emitting a set-cookie header each request. The behavior
# is controlled by the :meth:`should_set_cookie` method
# which performs a quick check to figure out if the cookie
# should be set or not. This is controlled by the
# SESSION_REFRESH_EACH_REQUEST config flag as well as
# the permanent flag on the session itself.
#if not self.should_set_cookie(app, session):
# return
httponly = self.get_cookie_httponly(app)
secure = self.get_cookie_secure(app)
expires = self.get_expiration_time(app, session)
#data = dict(session)
#self.cache.set(self.key_prefix + session.sid, data,
# int(app.permanent_session_lifetime.total_seconds()))
if self.use_signer: #签名加密
session_id = self._get_signer(app).sign(session.sid)
else:
session_id = session.sid
response.set_cookie(app.session_cookie_name, session_id,
expires=expires, httponly=httponly,
domain=domain, path=path, secure=secure)
def save_session_without_response(self, app, session):
#httponly = self.get_cookie_httponly(app)
#secure = self.get_cookie_secure(app)
#expires = self.get_expiration_time(app, session)
data = dict(session)
self.cache.set(self.key_prefix + session.sid, data,
int(app.permanent_session_lifetime.total_seconds()))
def judge_attack(self, app, request): #判断两次间隔的访问是否大于三秒
sid = request.cookies.get(app.session_cookie_name)
if not sid:
return False
current_time = time.time()
try:
last_time = self.cache.get(self.key_prefix + sid)['time'] #这是根据session_id获取session值
except:
return False
if current_time-last_time < 3:
return True
return False