1
0
Fork 0
mirror of https://gitlab.com/SIGBUS/nyaa.git synced 2024-12-22 14:49:59 +00:00

Add 1 hour cache to magnet URIs (#503)

Using Flask-Caching, we can memoize the magnet_uri method. Here, a
timeout of 1 hour is chosen, though that value can be fiddled with.

The cache is defined in extensions.py, but gets initialised in
__init__.py.
This commit is contained in:
Nicolas F 2018-07-10 10:22:49 +02:00 committed by Arylide
parent 5c8b119611
commit bd419c5d39
4 changed files with 9 additions and 2 deletions

View file

@ -6,7 +6,7 @@ import flask
from flask_assets import Bundle # noqa F401 from flask_assets import Bundle # noqa F401
from nyaa.api_handler import api_blueprint from nyaa.api_handler import api_blueprint
from nyaa.extensions import assets, db, fix_paginate, toolbar from nyaa.extensions import assets, cache, db, fix_paginate, toolbar
from nyaa.template_utils import bp as template_utils_bp from nyaa.template_utils import bp as template_utils_bp
from nyaa.utils import random_string from nyaa.utils import random_string
from nyaa.views import register_views from nyaa.views import register_views
@ -102,4 +102,7 @@ def create_app(config):
url = flask.url_for('static', filename='img/avatar/default.png', _external=True) url = flask.url_for('static', filename='img/avatar/default.png', _external=True)
app.config['DEFAULT_GRAVATAR_URL'] = url app.config['DEFAULT_GRAVATAR_URL'] = url
# Cache
cache.init_app(app)
return app return app

View file

@ -3,12 +3,14 @@ import os.path
from flask import abort from flask import abort
from flask.config import Config from flask.config import Config
from flask_assets import Environment from flask_assets import Environment
from flask_caching import Cache
from flask_debugtoolbar import DebugToolbarExtension from flask_debugtoolbar import DebugToolbarExtension
from flask_sqlalchemy import BaseQuery, Pagination, SQLAlchemy from flask_sqlalchemy import BaseQuery, Pagination, SQLAlchemy
assets = Environment() assets = Environment()
db = SQLAlchemy() db = SQLAlchemy()
toolbar = DebugToolbarExtension() toolbar = DebugToolbarExtension()
cache = Cache(config={'CACHE_TYPE': 'simple'})
def fix_paginate(): def fix_paginate():

View file

@ -16,7 +16,7 @@ from sqlalchemy.ext import declarative
from sqlalchemy_fulltext import FullText from sqlalchemy_fulltext import FullText
from sqlalchemy_utils import ChoiceType, EmailType, PasswordType from sqlalchemy_utils import ChoiceType, EmailType, PasswordType
from nyaa.extensions import config, db from nyaa.extensions import cache, config, db
from nyaa.torrents import create_magnet from nyaa.torrents import create_magnet
app = flask.current_app app = flask.current_app
@ -243,6 +243,7 @@ class TorrentBase(DeclarativeHelperBase):
return self.info_hash.hex() return self.info_hash.hex()
@property @property
@cache.memoize(timeout=3600)
def magnet_uri(self): def magnet_uri(self):
return create_magnet(self) return create_magnet(self)

View file

@ -53,3 +53,4 @@ visitor==0.1.3
webassets==0.12.1 webassets==0.12.1
Werkzeug==0.12.2 Werkzeug==0.12.2
WTForms==2.1 WTForms==2.1
Flask-Caching==1.4.0