class SitemapPlugin(plugins.SingletonPlugin): plugins.implements(plugins.IMiddleware, inherit=True) def make_middleware(self, app, config): """ Set up page caching for the sitemap. Caching needs access to the app and the view function or where the rule is added. CKAN loads the blueprints before the middleware, so it's impossible to use a normal blueprint function. https://docs.ckan.org/en/2.9/extensions/plugin-interfaces.html#ckan.plugins.interfaces.IMiddleware.make_middleware """ # Use a different redis DB than CKAN, incase it needs to be cleared redis_url, db = c.get('ckan.redis.url').rsplit('/', 1) cache_redis_url = f'{redis_url}/{int(db) + 1}' self.cache = Cache(config={'CACHE_TYPE': 'RedisCache', 'CACHE_REDIS_URL': cache_redis_url}) self.cache.init_app(app) cached_view = self.cache.cached(timeout=3600*24)(views.view) bp = Blueprint(u'sitemap', self.__module__) bp.add_url_rule(u'/sitemap.xml', view_func=cached_view) app.register_extension_blueprint(bp) return app
from flask import Flask, flash, redirect, render_template, request, url_for from flask_caching import Cache app = Flask(__name__) app.secret_key = 'some_secret' cache = Cache(config={'CACHE_TYPE': 'simple'}) cache.init_app(app) @app.route('/') def index(): return render_template('flash_index.html') cache.cached(timeout=3600) @app.route('/popular/<float:lat>/<float:long>') def popular_view(lat, long): return "Something using lat and long..." # @app.before_request # def blacklist(request): # if request.ip == '123.4.56.78': # redirect('/baduser', 403) @app.after_request def downgrade_heading(response):
sys.path.append(os.getcwd()) sys.path.append(os.getcwd() + "/api") # load config file with database credentials, Etc. config = json.load(open('./config.json')) # Init app = Flask(__name__) app.debug = True app.config['SECRET_KEY'] = config['auth_secret'] cache_config = {'CACHE_TYPE': 'filesystem', 'CACHE_DIR': 'tmp'} cache = Cache(app, config=cache_config) api = Api(app, decorators=[ cache.cached(timeout=43200, key_prefix=make_cache_key, unless=bypass_caching) ]) CORS(app) # emit banner api.add_resource(banner.banner, '/') api.add_resource(stats.stats, '/stats') api.add_resource(bug_report.bug_report, '/bug_report') api.add_resource(annotations.annotations, '/annotations') api.add_resource(es_occurrences.es_occurrences, '/occurrences') api.add_resource(es_publications.es_publications, '/publications') api.add_resource(annotations.single, '/annotations/single/<string:annotation_id>') api.add_resource(create_annotation.create_annotation, '/annotations/create') api.add_resource(oauth.oauth, '/oauth')
from flask_maple.models import db from flask_maple.redis import Redis from flask_maple.mail import Mail from flask_principal import Principal from flask_msearch import Search from flask_caching import Cache from . import babel, login, maple db = db csrf = CSRFProtect() redis_data = Redis() cache = Cache() mail = Mail() principal = Principal() search = Search(db=db) avatar = Avatar(cache=cache.cached( timeout=259200, key_prefix=lambda: "avatar:{}".format(request.url))) def init_app(app): db.init_app(app) cache.init_app(app) avatar.init_app(app) csrf.init_app(app) principal.init_app(app) redis_data.init_app(app) mail.init_app(app) search.init_app(app) babel.init_app(app) login.init_app(app) maple.init_app(app)