Skip to content

Commit 7079b6f

Browse files
committedNov 19, 2015
added: New API endpoint _cache_control. Solves scrapinghub#203
1 parent d95f3ee commit 7079b6f

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
 

‎docs/api.rst

+21
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,27 @@ To reclaim some RAM send a POST request to the ``/_gc`` endpoint::
938938

939939
It runs the Python garbage collector and clears internal WebKit caches.
940940

941+
.. _http-cache-control:
942+
943+
_cache_control
944+
~~~~~~~~~~~~~~
945+
946+
To control the internal memory cache used in WebKit, send a POST to the
947+
``/_cache_control`` endpoint.
948+
949+
Supported arguments:
950+
951+
* ``max_pages`` : Optional. Non-negative integer. Would be passed to `QWebSettings::setMaximumPagesInCache`_
952+
953+
* ``object_capacities``: Optional. Three non-negative integers separated by commas. Would be passed to `QWebSettings::setObjectCacheCapacities`_
954+
955+
.. _`QWebSettings::setMaximumPagesInCache`: http://doc.qt.io/qt-5/qwebsettings.html#setMaximumPagesInCache
956+
.. _`QWebSettings::setObjectCacheCapacities`: http://doc.qt.io/qt-5/qwebsettings.html#setObjectCacheCapacities
957+
958+
A sample to disable the internal WebKit memory cache::
959+
960+
curl -X POST http://localhost:8050/_cache_control --data max_pages=0 --data object_capacities=0,0,0
961+
941962
.. _http-debug:
942963

943964
_debug

‎splash/resources.py

+33
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from __future__ import absolute_import
66
import os
77
import gc
8+
import re
89
import time
910
import json
1011
import types
@@ -16,6 +17,7 @@
1617
from twisted.internet import reactor, defer
1718
from twisted.python import log
1819
import six
20+
from PyQt5.QtWebKit import QWebSettings
1921

2022
import splash
2123
from splash.qtrender import (
@@ -347,6 +349,36 @@ def render_POST(self, request):
347349
}).encode('utf-8')
348350

349351

352+
class CacheControlResource(Resource):
353+
isLeaf = True
354+
content_type = "application/json"
355+
356+
def render_POST(self, request):
357+
for key, pattern in (
358+
('max_pages', r'^\d+$'),
359+
('object_capacities', r'^\d+,\d+,\d+$'),
360+
):
361+
if request.args.get(key):
362+
data = ''.join(request.args[key])
363+
if not re.match(pattern, data):
364+
request.setResponseCode(400)
365+
return json.dumps({
366+
'status': 'error',
367+
'key': key,
368+
'supported_format': pattern,
369+
'received': data,
370+
})
371+
if request.args.get('max_pages'):
372+
data = ''.join(request.args['max_pages'])
373+
QWebSettings.setMaximumPagesInCache(int(data))
374+
if request.args.get('object_capacities'):
375+
data = ''.join(request.args['object_capacities']).split(',')
376+
QWebSettings.setObjectCacheCapacities(*[int(x) for x in data])
377+
return json.dumps({
378+
"status": "ok",
379+
}).encode('utf-8')
380+
381+
350382
BOOTSTRAP_THEME = 'simplex'
351383
CODEMIRROR_OPTIONS = """{
352384
mode: 'lua',
@@ -704,6 +736,7 @@ def __init__(self, pool, ui_enabled, lua_enabled, lua_sandbox_enabled,
704736

705737
self.putChild(b"_debug", DebugResource(pool))
706738
self.putChild(b"_gc", ClearCachesResource())
739+
self.putChild(b"_cache_control", CacheControlResource())
707740

708741
# backwards compatibility
709742
self.putChild(b"debug", DebugResource(pool, warn=True))

0 commit comments

Comments
 (0)