def get(key: str, cache_expire: Optional[int] = None, should_cache: bool = True) -> bytes: """Returns an object from storage Args: key: The key to get from storage cache_expire: The amount of time (in seconds) until the key expires if cache miss should_cache: Whether or not to fetch/save to/from cache Returns: data as bytes Raises: exceptions.NotFound exception if key is not found in storage exceptions.StorageError on any unexpected error interacting with storage """ try: obj = None if should_cache: obj = redis.cache_get(key) if not obj: obj = storage.get(STORAGE_LOCATION, key) if should_cache and len(obj) < CACHE_LIMIT: redis.cache_put(key, obj, cache_expire) return obj except exceptions.NotFound: raise except Exception: _log.exception("Uncaught exception while performing storage get") raise exceptions.StorageError( "Uncaught exception while performing storage get")
def select_transaction(block_id: str, txn_id: str, cache_expire: Optional[int] = None) -> dict: """Returns an transaction in a block from storage through the LRU cache block_id: The ID of the block being queried txn_id: The ID of the transaction in the block cache_expire: The amount of time (in seconds) until the key expires if cache miss Returns: transaction JSON object Raises: exceptions.NotFound exception when block id not found exceptions.StorageError on any unexpected error interacting with storage """ try: obj: Any = None key = f"{block_id}/{txn_id}" obj = redis.cache_get(key) if obj: return json.loads(obj) obj = storage.select_transaction(STORAGE_LOCATION, block_id, txn_id) cache_val = json.dumps(obj, separators=(",", ":")).encode("utf-8") if len(cache_val) < CACHE_LIMIT: redis.cache_put(key, cache_val, cache_expire) return obj except exceptions.NotFound: raise except Exception: _log.exception( "Uncaught exception while performing storage select_transaction") raise exceptions.StorageError( "Uncaught exception while performing storage select_transaction")
def put(key: str, value: bytes, cache_expire: Optional[int] = None, should_cache: bool = True) -> None: """Puts an object into storage with optional cache write-thru Args: key: The key of the object being written in S3 value: The value of the bytes object being written in S3 cache_expire: The amount of time (in seconds) until the key expires in the cache Raises: exceptions.StorageError on any unexpected error interacting with storage """ try: storage.put(STORAGE_LOCATION, key, value) if should_cache: redis.cache_put(key, value, cache_expire) except Exception: raise exceptions.StorageError( "Uncaught exception while performing storage put")
def test_cache_put_no_cache_expire(self): redis.cache_put("banana", "banana") redis.redis_client_lru.set.assert_called_once_with("storage:banana", "banana", ex=None)