|
| 1 | +# Copyright (c) 2023, Oracle and/or its affiliates. |
| 2 | +# Licensed under the Universal Permissive License v 1.0 as shown at |
| 3 | +# https://oss.oracle.com/licenses/upl. |
| 4 | + |
| 5 | +import asyncio |
| 6 | + |
| 7 | +from coherence import Filters, NamedMap, Session |
| 8 | +from coherence.event import MapLifecycleEvent, MapListener |
| 9 | +from coherence.filter import MapEventFilter |
| 10 | + |
| 11 | + |
| 12 | +async def do_run() -> None: |
| 13 | + """ |
| 14 | + Demonstrates listeners for entry events and cache lifecycle. |
| 15 | +
|
| 16 | + :return: None |
| 17 | + """ |
| 18 | + session: Session = Session() |
| 19 | + try: |
| 20 | + namedMap: NamedMap[int, str] = await session.get_map("listeners-map") |
| 21 | + await namedMap.put(1, "1") |
| 22 | + |
| 23 | + print("NamedMap lifecycle events") |
| 24 | + |
| 25 | + namedMap.on(MapLifecycleEvent.RELEASED, lambda x: print("RELEASED", x)) |
| 26 | + namedMap.on(MapLifecycleEvent.TRUNCATED, lambda x: print("TRUNCATE", x)) |
| 27 | + namedMap.on(MapLifecycleEvent.DESTROYED, lambda x: print("DESTROYED", x)) |
| 28 | + |
| 29 | + print("Truncating the NamedMap; this should generate an event ...") |
| 30 | + await namedMap.truncate() |
| 31 | + await asyncio.sleep(1) |
| 32 | + |
| 33 | + print("Releasing the NamedMap; this should generate an event ...") |
| 34 | + namedMap.release() |
| 35 | + await asyncio.sleep(1) |
| 36 | + |
| 37 | + print("Destroying the NamedMap; this should generate an event ...") |
| 38 | + await namedMap.destroy() |
| 39 | + await asyncio.sleep(1) |
| 40 | + |
| 41 | + print("\n\nNamedMap eventy events") |
| 42 | + |
| 43 | + namedMap = await session.get_map("listeners-map") |
| 44 | + |
| 45 | + listener1: MapListener[int, str] = MapListener() |
| 46 | + listener1.on_any(lambda e: print(e)) |
| 47 | + |
| 48 | + print("Added listener for all events") |
| 49 | + print("Events will be generated when an entry is inserted, updated, and removed") |
| 50 | + await namedMap.add_map_listener(listener1) |
| 51 | + |
| 52 | + await namedMap.put(1, "1") |
| 53 | + await namedMap.put(1, "2") |
| 54 | + await namedMap.remove(1) |
| 55 | + await asyncio.sleep(1) |
| 56 | + |
| 57 | + await namedMap.remove_map_listener(listener1) |
| 58 | + |
| 59 | + print("\nAdded listener for all entries, but only when they are inserted") |
| 60 | + filter = Filters.event(Filters.always(), MapEventFilter.INSERTED) |
| 61 | + await namedMap.add_map_listener(listener1, filter) |
| 62 | + |
| 63 | + await namedMap.put(1, "1") |
| 64 | + await namedMap.put(1, "2") |
| 65 | + await namedMap.remove(1) |
| 66 | + await asyncio.sleep(1) |
| 67 | + |
| 68 | + await namedMap.remove_map_listener(listener1, filter) |
| 69 | + |
| 70 | + print("\nAdded listener for entries with a length larger than one, but only when they are updated or removed") |
| 71 | + filter = Filters.event(Filters.greater("length()", 1), MapEventFilter.UPDATED | MapEventFilter.DELETED) |
| 72 | + await namedMap.add_map_listener(listener1, filter) |
| 73 | + |
| 74 | + for i in range(12): |
| 75 | + await namedMap.put(i, str(i)) |
| 76 | + await namedMap.put(i, str(i + 1)) |
| 77 | + await namedMap.remove(i) |
| 78 | + |
| 79 | + await asyncio.sleep(1) |
| 80 | + |
| 81 | + await namedMap.remove_map_listener(listener1, filter) |
| 82 | + await namedMap.clear() |
| 83 | + finally: |
| 84 | + await session.close() |
| 85 | + |
| 86 | + |
| 87 | +asyncio.run(do_run()) |
0 commit comments