|
| 1 | +from __future__ import annotations |
| 2 | +from typing import TYPE_CHECKING, Union |
| 3 | + |
| 4 | +if TYPE_CHECKING: |
| 5 | + from deriv_api import DerivAPI |
| 6 | +from deriv_api.deriv_api_calls import DerivAPICalls |
| 7 | +from deriv_api.errors import ConstructionError |
| 8 | +from deriv_api.utils import dict_to_cache_key |
| 9 | +from deriv_api.in_memory import InMemory |
| 10 | + |
| 11 | +__pdoc__ = { |
| 12 | + 'deriv_api.cache.Cache.get': False, |
| 13 | + 'deriv_api.cache.Cache.get_by_msg_type': False, |
| 14 | + 'deriv_api.cache.Cache.has': False, |
| 15 | + 'deriv_api.cache.Cache.send': False, |
| 16 | + 'deriv_api.cache.Cache.set': False |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +class Cache(DerivAPICalls): |
| 21 | + """ |
| 22 | + Cache - A class for implementing in-memory and persistent cache |
| 23 | +
|
| 24 | + The real implementation of the underlying cache is delegated to the storage |
| 25 | + object (See the params). |
| 26 | +
|
| 27 | + The storage object needs to implement the API. |
| 28 | +
|
| 29 | + Examples |
| 30 | + -------- |
| 31 | + - Read the latest active symbols |
| 32 | + >>> symbols = await api.active_symbols() |
| 33 | +
|
| 34 | + - Read the data from cache if available |
| 35 | + >>> cached_symbols = await api.cache.active_symbols() |
| 36 | +
|
| 37 | + Parameters |
| 38 | + ---------- |
| 39 | + api : deriv_api.DerivAPI |
| 40 | + API instance to get data that is not cached |
| 41 | + storage : Object |
| 42 | + A storage instance to use for caching |
| 43 | + """ |
| 44 | + |
| 45 | + def __init__(self, api: Union[DerivAPI, Cache], storage: Union[InMemory, Cache]) -> None: |
| 46 | + if not api: |
| 47 | + raise ConstructionError('Cache object needs an API to work') |
| 48 | + |
| 49 | + super().__init__() |
| 50 | + self.api = api |
| 51 | + self.storage = storage |
| 52 | + |
| 53 | + async def send(self, request: dict) -> dict: |
| 54 | + """Check if there is a cache for the request. If so then return that value. |
| 55 | + Otherwise send the request by the api |
| 56 | +
|
| 57 | + Parameters |
| 58 | + ---------- |
| 59 | + request : dict |
| 60 | + API request |
| 61 | +
|
| 62 | + Returns |
| 63 | + ------- |
| 64 | + API Response |
| 65 | + """ |
| 66 | + if await self.has(request): |
| 67 | + return await self.get(request) |
| 68 | + |
| 69 | + response = await self.api.send(request) |
| 70 | + self.set(request, response) |
| 71 | + return response |
| 72 | + |
| 73 | + async def has(self, request: dict) -> bool: |
| 74 | + """Redirected to the method defined by the storage |
| 75 | +
|
| 76 | + Parameters |
| 77 | + ---------- |
| 78 | + request : dict |
| 79 | + API request |
| 80 | +
|
| 81 | + Returns |
| 82 | + ------- |
| 83 | + Returns true if the request exists |
| 84 | + """ |
| 85 | + return self.storage.has(dict_to_cache_key(request)) |
| 86 | + |
| 87 | + async def get(self, request: dict) -> dict: |
| 88 | + """Redirected to the method defined by the storage |
| 89 | +
|
| 90 | + Parameters |
| 91 | + ---------- |
| 92 | + request : dict |
| 93 | + API request |
| 94 | +
|
| 95 | + Returns |
| 96 | + ------- |
| 97 | + API response stored in |
| 98 | + """ |
| 99 | + return self.storage.get(dict_to_cache_key(request)) |
| 100 | + |
| 101 | + async def get_by_msg_type(self, msg_type: str) -> dict: |
| 102 | + """Redirected to the method defined by the storage |
| 103 | +
|
| 104 | + Parameters |
| 105 | + ---------- |
| 106 | + msg_type : str |
| 107 | + Request msg_type |
| 108 | +
|
| 109 | + Returns |
| 110 | + ------- |
| 111 | + Returns response stored in |
| 112 | + """ |
| 113 | + return self.storage.get_by_msg_type(msg_type) |
| 114 | + |
| 115 | + def set(self, request: dict, response: dict) -> None: |
| 116 | + """Redirected to the method defined by the storage |
| 117 | +
|
| 118 | + Parameters |
| 119 | + ---------- |
| 120 | + request : dict |
| 121 | + API request |
| 122 | + response : dict |
| 123 | + API response |
| 124 | + """ |
| 125 | + return self.storage.set(dict_to_cache_key(request), response) |
0 commit comments