Caching layer ¶
The caching layer wraps a backing redis service and provides a simple, focused interface to its usage.
pems_data.cache
¶
The primary caching interface for pems_data
.
Cache
¶
Basic wrapper for a cache backend.
Source code in pems_data/cache.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
|
__init__(host=None, port=None)
¶
Create a new instance of the Cache interface.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
host
|
str
|
(Optional) The hostname of the cache backend |
None
|
port
|
int
|
(Optional) The port to connect on the cache backend |
None
|
Source code in pems_data/cache.py
58 59 60 61 62 63 64 65 66 67 68 |
|
build_key(*args)
classmethod
¶
Build a standard cache key from the given parts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
tuple[Any]
|
The individual parts that make up the key |
()
|
Returns:
Name | Type | Description |
---|---|---|
value |
str
|
A standard representation of the parts for use in a cache key. |
Source code in pems_data/cache.py
46 47 48 49 50 51 52 53 54 55 56 |
|
get(key, mutate_func=None)
¶
Get a raw value from the cache, or None if the key doesn’t exist.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The item’s cache key |
required |
mutate_func
|
callable
|
(Optional) If provided, call this on the cached value and return its result |
None
|
Returns:
Name | Type | Description |
---|---|---|
value |
Any | None
|
The value from the cache, optionally mutated by mutate_func, or None. |
Source code in pems_data/cache.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
|
get_df(key)
¶
Get a DataFrame from the cache, or an empty DataFrame if the key wasn’t found.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The item’s cache key |
required |
Returns:
Name | Type | Description |
---|---|---|
value |
DataFrame
|
The DataFrame materialized from the cache, or an empty DataFrame if the key wasn’t found. |
Source code in pems_data/cache.py
106 107 108 109 110 111 112 113 114 115 |
|
is_available()
¶
Return a bool indicating if the cache backend is available or not.
Returns:
Name | Type | Description |
---|---|---|
value |
bool
|
True if the connection and backend is available; False otherwise |
Source code in pems_data/cache.py
75 76 77 78 79 80 81 82 83 84 |
|
set(key, value, ttl=None, mutate_func=None)
¶
Set a value in the cache, with an optional TTL (seconds until expiration).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The item’s cache key |
required |
value
|
Any
|
The item’s value to store in the cache |
required |
ttl
|
int
|
(Optional) Seconds until expiration |
None
|
mutate_func
|
callable
|
(Optional) If provided, call this on the value and insert the result in the cache |
None
|
Source code in pems_data/cache.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
set_df(key, value, ttl=None)
¶
Set a DataFrame in the cache, with an optional TTL (seconds until expiration).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The item’s cache key |
required |
value
|
Any
|
The DataFrame to store in the cache |
required |
ttl
|
int
|
(Optional) Seconds until expiration |
None
|
Source code in pems_data/cache.py
135 136 137 138 139 140 141 142 143 |
|
redis_connection(host=None, port=None, **kwargs)
¶
Try to create a new connection to a redis backend. Return None if the connection fails.
Uses the REDIS_HOSTNAME
and REDIS_PORT
environment variables as fallback.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
host
|
str
|
(Optional) The redis hostname |
None
|
port
|
int
|
(Optional) The port to connect on |
None
|
Returns:
Name | Type | Description |
---|---|---|
value |
Redis
|
A Redis instance connected to |
Source code in pems_data/cache.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
pems_data.serialization
¶
Helpers for serializing specific data types e.g. for storing/retrieving from a cache.
arrow_bytes_to_df(arrow_buffer)
¶
Deserializes Arrow IPC format bytes back to a DataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arrow_buffer
|
bytes
|
A buffer of Arrow IPC bytes representing a DataFrame |
required |
Returns:
Name | Type | Description |
---|---|---|
value |
DataFrame
|
The DataFrame deserialized from the buffer, or an empty DataFrame if the buffer was empty. |
Source code in pems_data/serialization.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
df_to_arrow_bytes(df)
¶
Serializes a DataFrame to Arrow IPC format bytes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
The DataFrame to serialize |
required |
Returns:
Name | Type | Description |
---|---|---|
value |
bytes
|
The Arrow IPC format bytes representation of the DataFrame. |
Source code in pems_data/serialization.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|