Naive store class which can be used for the session middleware and unit tests. It is not thread-safe and no data will survive the lifecycle of the hug process. Regard this as a blueprint for more useful and probably more complex store implementations, for example stores which make u
| 23 | |
| 24 | |
| 25 | class InMemoryStore: |
| 26 | """ |
| 27 | Naive store class which can be used for the session middleware and unit tests. |
| 28 | It is not thread-safe and no data will survive the lifecycle of the hug process. |
| 29 | Regard this as a blueprint for more useful and probably more complex store implementations, for example stores |
| 30 | which make use of databases like Redis, PostgreSQL or others. |
| 31 | """ |
| 32 | |
| 33 | def __init__(self): |
| 34 | self._data = {} |
| 35 | |
| 36 | def get(self, key): |
| 37 | """Get data for given store key. Raise hug.exceptions.StoreKeyNotFound if key does not exist.""" |
| 38 | try: |
| 39 | data = self._data[key] |
| 40 | except KeyError: |
| 41 | raise StoreKeyNotFound(key) |
| 42 | return data |
| 43 | |
| 44 | def exists(self, key): |
| 45 | """Return whether key exists or not.""" |
| 46 | return key in self._data |
| 47 | |
| 48 | def set(self, key, data): |
| 49 | """Set data object for given store key.""" |
| 50 | self._data[key] = data |
| 51 | |
| 52 | def delete(self, key): |
| 53 | """Delete data for given store key.""" |
| 54 | if key in self._data: |
| 55 | del self._data[key] |
no outgoing calls