Events
Coherence provides the ability to subscribe to notifications pertaining to a particular map/cache. In addition to listening for specific events, it is possible to listen to events for changes made to a specific key, or using a Filter, it’s possible to limit the events raised to be for a subset of the map entries.
The following example demonstrates using lifecycle and map events.
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
5import asyncio
6
7from coherence import Filters, NamedMap, Session
8from coherence.event import MapLifecycleEvent, MapListener
9from coherence.filter import MapEventFilter
10
11
12async def do_run() -> None:
13 """
14 Demonstrates listeners for entry events and cache lifecycle.
15
16 :return: None
17 """
18 session: Session = await Session.create()
19 try:
20 named_map: NamedMap[int, str] = await session.get_map("listeners-map")
21 await named_map.put(1, "1")
22
23 print("NamedMap lifecycle events")
24
25 named_map.on(MapLifecycleEvent.RELEASED, lambda x: print("RELEASED", x))
26 named_map.on(MapLifecycleEvent.TRUNCATED, lambda x: print("TRUNCATE", x))
27 named_map.on(MapLifecycleEvent.DESTROYED, lambda x: print("DESTROYED", x))
28
29 print("Truncating the NamedMap; this should generate an event ...")
30 await named_map.truncate()
31 await asyncio.sleep(1)
32
33 print("Releasing the NamedMap; this should generate an event ...")
34 named_map.release()
35 await asyncio.sleep(1)
36
37 print("Destroying the NamedMap; this should generate an event ...")
38 await named_map.destroy()
39 await asyncio.sleep(1)
40
41 print("\n\nNamedMap entry events")
42
43 named_map = 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 named_map.add_map_listener(listener1)
51
52 await named_map.put(1, "1")
53 await named_map.put(1, "2")
54 await named_map.remove(1)
55 await asyncio.sleep(1)
56
57 await named_map.remove_map_listener(listener1)
58
59 print("\nAdded listener for all entries, but only when they are inserted")
60 ins_filter = Filters.event(Filters.always(), MapEventFilter.INSERTED)
61 await named_map.add_map_listener(listener1, ins_filter)
62
63 await named_map.put(1, "1")
64 await named_map.put(1, "2")
65 await named_map.remove(1)
66 await asyncio.sleep(1)
67
68 await named_map.remove_map_listener(listener1, ins_filter)
69
70 print("\nAdded listener for entries with a length larger than one, but only when they are updated or removed")
71 upd_del_filter = Filters.event(Filters.greater("length()", 1), MapEventFilter.UPDATED | MapEventFilter.DELETED)
72 await named_map.add_map_listener(listener1, upd_del_filter)
73
74 for i in range(12):
75 await named_map.put(i, str(i))
76 await named_map.put(i, str(i + 1))
77 await named_map.remove(i)
78
79 await asyncio.sleep(1)
80
81 await named_map.remove_map_listener(listener1, upd_del_filter)
82 await named_map.clear()
83 finally:
84 await session.close()
85
86
87asyncio.run(do_run())
Lines 25-39 - Using NamedMap.on(), define listeners supported events and pass in a lambda to print the invocation. Then one by one, trigger each of the events and ensure enough time is given for the event to occur.
Lines 45-46 - Create a new MapListener and for any event, print the result.
Lines 50-54 - Add the MapListener that will be triggered on all events For this section, events will be printed for inserted, updated, and deleted.
Lines 60-66 - Add the MapListener that will be triggered when any entry is inserted. For this section, only the inserted event will be printed.
71-77 - Add the MapListener that will be triggered when an entry’s value’s length is greater than 1 and only for updated and removed events. For this section, only updated and deleted events will be printed once the loop progresses enough to insert values larger than “9”