Skip to content
Snippets Groups Projects
Commit 908247a6 authored by Dan Notestein's avatar Dan Notestein Committed by root
Browse files

Change function of -2 caching (previously called...

Change function of -2 caching (previously called DEFAULT_EXPIRE_IF_IRREVRESIBLE, now called EXPIRE IF REVERSIBLE). This caching is mainly used for caching blocks and block headers. Now irreversible blocks don't have an expiration time, so they only get evicted when the cache fills up. And the default expiration time for reversible blocks is 9s (EXTENDED_TTL)
parent e4ac227c
No related branches found
No related tags found
2 merge requests!9finally merge old fixes with master,!7merge changes from hbt4
......@@ -33,7 +33,7 @@ from .utils import merge_cached_responses
logger = structlog.getLogger(__name__)
BATCH_IRREVERSIBLE_TTL_SET = frozenset([TTL.DEFAULT_EXPIRE_IF_IRREVERSIBLE])
BATCH_IRREVERSIBLE_TTL_SET = frozenset([TTL.EXPIRE_IF_REVERSIBLE])
# types
CacheTTLValue = TypeVar('CacheTTL', int, float, type(None))
......@@ -188,13 +188,14 @@ class CacheGroup:
) -> None:
key = jsonrpc_cache_key(request)
ttl = ttl or request.upstream.ttl
if ttl == TTL.DEFAULT_EXPIRE_IF_IRREVERSIBLE:
if ttl == TTL.EXPIRE_IF_REVERSIBLE:
last_irreversible_block_num = last_irreversible_block_num or \
self._memory_cache.gets('last_irreversible_block_num') or \
await self.get('last_irreversible_block_num')
ttl = irreversible_ttl(jsonrpc_response=response,
last_irreversible_block_num=last_irreversible_block_num)
last_irreversible_block_num=last_irreversible_block_num,
request=request)
elif ttl == TTL.NO_CACHE:
return
value = self.prepare_response_for_cache(request, response)
......@@ -218,7 +219,7 @@ class CacheGroup:
else:
new_ttls = []
for i, ttl in enumerate(ttls):
if ttl == TTL.DEFAULT_EXPIRE_IF_IRREVERSIBLE:
if ttl == TTL.EXPIRE_IF_REVERSIBLE:
ttl = irreversible_ttl(responses[i], last_irreversible_block_num)
new_ttls.append(ttl)
triplets = filter(lambda p: p[0] != TTL.NO_CACHE, zip(ttls, requests, responses))
......
......@@ -12,7 +12,7 @@ Method Settings
- For readabilty/writabilty, there are shorthand variables for these 'special' TTL values:
- `NO_EXPIRE` == 0
- `NO_CACHE` == -1
- `DEFAULT_EXPIRE_IF_IRREVERSIBLE` == -2
- `EXPIRE_IF_REVERSIBLE` == -2
"""
......@@ -21,9 +21,10 @@ from enum import Enum
class TTL(Enum):
DEFAULT_TTL = 3
EXTENDED_TTL = 9
NO_EXPIRE = None
NO_CACHE = -1
DEFAULT_EXPIRE_IF_IRREVERSIBLE = -2
EXPIRE_IF_REVERSIBLE = -2
# pylint: disable=no-else-return
def __eq__(self, other: int) -> bool:
......
......@@ -21,7 +21,8 @@ def jsonrpc_cache_key(single_jsonrpc_request: SingleJrpcRequest) -> str:
def irreversible_ttl(jsonrpc_response: dict=None,
last_irreversible_block_num: int=None) -> TTL:
last_irreversible_block_num: int=None,
request: SingleJrpcRequest=None) -> TTL:
if not jsonrpc_response:
return TTL.NO_CACHE
if not isinstance(last_irreversible_block_num, int):
......@@ -30,12 +31,21 @@ def irreversible_ttl(jsonrpc_response: dict=None,
return TTL.NO_CACHE
try:
jrpc_block_num = block_num_from_jsonrpc_response(jsonrpc_response)
return TTL.DEFAULT_TTL
if jrpc_block_num > last_irreversible_block_num:
return TTL.EXTENDED_TTL
else:
return TTL.NO_EXPIRE
except Exception as e:
if request is not None:
request_string = request.json()
else:
request_string = 'None'
logger.warning(
'Unable to cache using last irreversible block',
e=e,
lirb=last_irreversible_block_num)
lirb=last_irreversible_block_num,
request_string=request_string,
jsonrpc_response=jsonrpc_response)
return TTL.NO_CACHE
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment