Added caching
This commit is contained in:
parent
d9b963b22a
commit
8faf68dd89
|
@ -1,4 +1,17 @@
|
|||
from flask import Flask
|
||||
from flask_caching import Cache
|
||||
|
||||
cache_config = {
|
||||
"CACHE_TYPE": "memcached",
|
||||
"CACHE_DEFAULT_TIMEOUT": 10,
|
||||
"CACHE_MEMCACHED_SERVERS": "cache"
|
||||
}
|
||||
cache = Cache(config=cache_config)
|
||||
|
||||
|
||||
def get_cache(app):
|
||||
cache.init_app(app)
|
||||
return cache
|
||||
|
||||
|
||||
def create_app():
|
||||
|
@ -11,3 +24,4 @@ def create_app():
|
|||
|
||||
|
||||
app = create_app()
|
||||
cache.init_app(app)
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
""" pointer.rodeo tools
|
||||
"""
|
||||
|
||||
from app import app, get_cache
|
||||
|
||||
MAX_PATTERN_SIZE = 20000
|
||||
|
||||
CHAR1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||
|
@ -8,6 +10,7 @@ CHAR2 = 'abcdefghijklmnopqrstuvwxyz'
|
|||
CHAR3 = '0123456789'
|
||||
|
||||
|
||||
@get_cache(app).memoize(50)
|
||||
def create_pattern(size):
|
||||
pattern = []
|
||||
|
||||
|
@ -30,6 +33,7 @@ def create_pattern(size):
|
|||
return ''.join(pattern)
|
||||
|
||||
|
||||
@get_cache(app).memoize(50)
|
||||
def find_pattern(needle):
|
||||
pattern = create_pattern(MAX_PATTERN_SIZE)
|
||||
return str(pattern.index(needle))
|
||||
|
|
|
@ -2,7 +2,7 @@ from flask import (
|
|||
Blueprint, render_template, abort
|
||||
)
|
||||
|
||||
from app import rodeo
|
||||
from app import cache, rodeo
|
||||
|
||||
|
||||
POINTER_FORMAT = ('c', 'hex')
|
||||
|
@ -17,7 +17,10 @@ def index():
|
|||
|
||||
|
||||
@bp.route('/pat/<int:size>', methods=('GET',))
|
||||
@get_cache(app).memoize(50)
|
||||
def pattern_create(size):
|
||||
if size > 16384:
|
||||
abort(413)
|
||||
pattern = rodeo.create_pattern(size)
|
||||
if not pattern:
|
||||
abort(411)
|
||||
|
@ -25,6 +28,7 @@ def pattern_create(size):
|
|||
|
||||
|
||||
@bp.route('/pat/<pattern>', methods=('POST',))
|
||||
@get_cache(app).memoize(50)
|
||||
def pattern_offset(pattern):
|
||||
if pattern.startswith('0x'):
|
||||
pattern = bytes.fromhex(pattern[2:]).decode('utf-8')[::-1]
|
||||
|
@ -34,6 +38,7 @@ def pattern_offset(pattern):
|
|||
@bp.route('/ptr', methods=('GET',))
|
||||
@bp.route('/ptr/<ord>/<fmt>', methods=('GET',))
|
||||
@bp.route('/ptr/<ordr>/rand/<fmt>', methods=('GET',))
|
||||
@get_cache(app).memoize(50)
|
||||
def pointer_random(ordr='l', fmt='c'):
|
||||
if fmt.lower() not in POINTER_FORMAT:
|
||||
abort(411)
|
||||
|
|
|
@ -6,7 +6,7 @@ services:
|
|||
container_name: pointer_rodeo
|
||||
restart: unless-stopped
|
||||
networks:
|
||||
- proxy
|
||||
- proxy
|
||||
labels:
|
||||
traefik.enable: true
|
||||
traefik.docker.network: proxy
|
||||
|
@ -15,6 +15,12 @@ services:
|
|||
traefik.http.routers.pointer_rodeo-http.service: pointer_rodeo
|
||||
traefik.http.services.pointer_rodeo.loadbalancer.server.port: 8000
|
||||
|
||||
cache:
|
||||
image: memcached:1.6
|
||||
restart: unless-stopped
|
||||
command:
|
||||
- '-m 32'
|
||||
|
||||
networks:
|
||||
proxy:
|
||||
external: true
|
||||
|
|
|
@ -1,3 +1,11 @@
|
|||
[[package]]
|
||||
name = "cachelib"
|
||||
version = "0.9.0"
|
||||
description = "A collection of cache libraries in the same API interface."
|
||||
category = "main"
|
||||
optional = false
|
||||
python-versions = ">=3.7"
|
||||
|
||||
[[package]]
|
||||
name = "click"
|
||||
version = "8.1.3"
|
||||
|
@ -35,6 +43,18 @@ Werkzeug = ">=2.2.2"
|
|||
async = ["asgiref (>=3.2)"]
|
||||
dotenv = ["python-dotenv"]
|
||||
|
||||
[[package]]
|
||||
name = "flask-caching"
|
||||
version = "2.0.1"
|
||||
description = "Adds caching support to Flask applications."
|
||||
category = "main"
|
||||
optional = false
|
||||
python-versions = ">=3.7"
|
||||
|
||||
[package.dependencies]
|
||||
cachelib = ">=0.9.0"
|
||||
Flask = "<3"
|
||||
|
||||
[[package]]
|
||||
name = "gunicorn"
|
||||
version = "20.1.0"
|
||||
|
@ -96,15 +116,17 @@ watchdog = ["watchdog"]
|
|||
[metadata]
|
||||
lock-version = "1.1"
|
||||
python-versions = "^3.10"
|
||||
content-hash = "8aae4d25363e5f924eb7bd97e9992c277507ca9b14488ffb73510506a2ee3a54"
|
||||
content-hash = "556ffa90cf28c71fa123e73dc06d4e93f9e229c919afe3a94a165da83844520f"
|
||||
|
||||
[metadata.files]
|
||||
cachelib = []
|
||||
click = [
|
||||
{file = "click-8.1.3-py3-none-any.whl", hash = "sha256:bb4d8133cb15a609f44e8213d9b391b0809795062913b383c62be0ee95b1db48"},
|
||||
{file = "click-8.1.3.tar.gz", hash = "sha256:7682dc8afb30297001674575ea00d1814d808d6a36af415a82bd481d37ba7b8e"},
|
||||
]
|
||||
colorama = []
|
||||
flask = []
|
||||
flask-caching = []
|
||||
gunicorn = []
|
||||
itsdangerous = [
|
||||
{file = "itsdangerous-2.1.2-py3-none-any.whl", hash = "sha256:2c2349112351b88699d8d4b6b075022c0808887cb7ad10069318a8b0bc88db44"},
|
||||
|
|
|
@ -11,6 +11,7 @@ gunicorn = "^20.1.0"
|
|||
Jinja2 = "^3.1.2"
|
||||
MarkupSafe = "^2.1.1"
|
||||
Werkzeug = "^2.2.2"
|
||||
Flask-Caching = "^2.0.1"
|
||||
|
||||
[tool.poetry.dev-dependencies]
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
cachelib==0.9.0; python_version >= "3.7"
|
||||
click==8.1.3; python_version >= "3.7"
|
||||
colorama==0.4.6; python_version >= "3.7" and python_full_version < "3.0.0" and platform_system == "Windows" or platform_system == "Windows" and python_version >= "3.7" and python_full_version >= "3.7.0"
|
||||
flask-caching==2.0.1; python_version >= "3.7"
|
||||
flask==2.2.2; python_version >= "3.7"
|
||||
gunicorn==20.1.0; python_version >= "3.5"
|
||||
itsdangerous==2.1.2; python_version >= "3.7"
|
||||
|
|
Loading…
Reference in New Issue