Kind-of Set (or priority queue) with limitations. Good for when you need to test for membership (`a in set`), but the set should not grow unbounded. ``maxlen`` is enforced at all times, so if the limit is reached we'll also remove non-expired items. You can also configure ``mi
| 439 | |
| 440 | |
| 441 | class LimitedSet: |
| 442 | """Kind-of Set (or priority queue) with limitations. |
| 443 | |
| 444 | Good for when you need to test for membership (`a in set`), |
| 445 | but the set should not grow unbounded. |
| 446 | |
| 447 | ``maxlen`` is enforced at all times, so if the limit is reached |
| 448 | we'll also remove non-expired items. |
| 449 | |
| 450 | You can also configure ``minlen``: this is the minimal residual size |
| 451 | of the set. |
| 452 | |
| 453 | All arguments are optional, and no limits are enabled by default. |
| 454 | |
| 455 | Arguments: |
| 456 | maxlen (int): Optional max number of items. |
| 457 | Adding more items than ``maxlen`` will result in immediate |
| 458 | removal of items sorted by oldest insertion time. |
| 459 | |
| 460 | expires (float): TTL for all items. |
| 461 | Expired items are purged as keys are inserted. |
| 462 | |
| 463 | minlen (int): Minimal residual size of this set. |
| 464 | .. versionadded:: 4.0 |
| 465 | |
| 466 | Value must be less than ``maxlen`` if both are configured. |
| 467 | |
| 468 | Older expired items will be deleted, only after the set |
| 469 | exceeds ``minlen`` number of items. |
| 470 | |
| 471 | data (Sequence): Initial data to initialize set with. |
| 472 | Can be an iterable of ``(key, value)`` pairs, |
| 473 | a dict (``{key: insertion_time}``), or another instance |
| 474 | of :class:`LimitedSet`. |
| 475 | |
| 476 | Example: |
| 477 | >>> s = LimitedSet(maxlen=50000, expires=3600, minlen=4000) |
| 478 | >>> for i in range(60000): |
| 479 | ... s.add(i) |
| 480 | ... s.add(str(i)) |
| 481 | ... |
| 482 | >>> 57000 in s # last 50k inserted values are kept |
| 483 | True |
| 484 | >>> '10' in s # '10' did expire and was purged from set. |
| 485 | False |
| 486 | >>> len(s) # maxlen is reached |
| 487 | 50000 |
| 488 | >>> s.purge(now=time.monotonic() + 7200) # clock + 2 hours |
| 489 | >>> len(s) # now only minlen items are cached |
| 490 | 4000 |
| 491 | >>>> 57000 in s # even this item is gone now |
| 492 | False |
| 493 | """ |
| 494 | |
| 495 | max_heap_percent_overload = 15 |
| 496 | |
| 497 | def __init__(self, maxlen=0, expires=0, data=None, minlen=0): |
| 498 | # type: (int, float, Mapping, int) -> None |
no outgoing calls