BufferMap
This class provides an ordered mapping of message buffers with built-in eviction capabilities. It manages a collection of buffers, tracking the total number of items across all keys and enforcing a maximum size through a least-recently-used (LRU) eviction policy. The class supports adding single items or iterables to specific buffers while automatically handling buffer creation and capacity limits.
Attributes
| Attribute | Type | Description |
|---|---|---|
| Buffer | class = Messagebuffer | The class type used to instantiate new message buffers within the map. |
| Empty | exception = Empty | Exception class raised when attempting to take an item from an empty buffer. |
| maxsize | int = None | Maximum total number of items allowed across all buffers before eviction is triggered. |
| total | int = 0 | The current aggregate count of all items stored across every buffer in the map. |
| bufmaxsize | int = None | The maximum capacity assigned to each individual Messagebuffer instance created by the map. |
Constructor
Signature
def BufferMap(
maxsize: int,
iterable: Iterable = None,
bufmaxsize: int = 1000
) - > None
Parameters
| Name | Type | Description |
|---|---|---|
| maxsize | int | The maximum number of items allowed in the map before eviction occurs. |
| iterable | Iterable = None | An optional collection of items to initialize the buffer map with. |
| bufmaxsize | int = 1000 | The maximum size for individual buffers within the map. |
Methods
put()
@classmethod
def put(
key: Any,
item: Any
) - > null
Adds a single item to the buffer associated with the specified key and updates the map's total count. This operation marks the key as the most recently used and triggers eviction if the map exceeds its maximum size.
Parameters
| Name | Type | Description |
|---|---|---|
| key | Any | The identifier for the specific buffer where the item should be stored. |
| item | Any | The data element to be appended to the buffer. |
Returns
| Type | Description |
|---|---|
null | Nothing is returned. |
extend()
@classmethod
def extend(
key: Any,
it: Iterable
) - > null
Appends multiple items from an iterable to the buffer associated with the specified key. The total count is incremented by the number of items added, and eviction is performed if necessary.
Parameters
| Name | Type | Description |
|---|---|---|
| key | Any | The identifier for the specific buffer to be extended. |
| it | Iterable | A collection of items to be added to the buffer. |
Returns
| Type | Description |
|---|---|
null | Nothing is returned. |
take()
@classmethod
def take(
key: Any,
*default: Any
) - > Any
Retrieves and removes an item from the buffer associated with the key, marking it as recently used. If the buffer is empty or the key does not exist, it returns the provided default or raises an Empty exception.
Parameters
| Name | Type | Description |
|---|---|---|
| key | Any | The identifier for the buffer from which to retrieve an item. |
| *default | Any | An optional value to return if the key is missing or the buffer is empty. |
Returns
| Type | Description |
|---|---|
Any | The item removed from the buffer, or the default value if provided. |