aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/examples/plugins/cachetool.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/plugins/cachetool.py')
-rw-r--r--examples/plugins/cachetool.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/examples/plugins/cachetool.py b/examples/plugins/cachetool.py
new file mode 100644
index 0000000..77885d2
--- /dev/null
+++ b/examples/plugins/cachetool.py
@@ -0,0 +1,33 @@
+from cachetools import cached
+import time
+
+from iamai import Plugin
+from iamai.event import MessageEvent
+
+
+class CachedPlugin(Plugin):
+ async def handle(self) -> None:
+ # without cached
+ def fib(n):
+ return n if n < 2 else fib(n - 1) + fib(n - 2)
+
+ s = time.time()
+ await self.event.reply(f"{fib(36)}")
+ await self.event.reply(f"Time Taken: {time.time() - s}")
+
+ # Now using cached
+ s = time.time()
+
+ # Use this decorator to enable caching
+ @cached(cache={})
+ def fib(n):
+ return n if n < 2 else fib(n - 1) + fib(n - 2)
+
+ await self.event.reply(f"{fib(36)}")
+ await self.event.reply(f"Time Taken(cached): {time.time() - s}")
+
+ async def rule(self) -> bool:
+ return (
+ isinstance(self.event, MessageEvent)
+ and self.event.get_plain_text() == ".cachetools"
+ )