Caching
Commands must be executed quickly within 100 - 250 ms. If commands take a long time to complete, users have a bad impression of using the bot
Some commands cannot be executed quickly:
HTTP loading
long api requests (for example, sendVideo, getChatMember- all get methods and etc)
mass message broadcasting
Bot.runAll
Some commands can be very large or poorly coded:
mass properting reading/setting (more then 20 properties in one command
big (or infinite!) loops
etc
if your code runs slower than 300ms - necessary:
try to make it more quickly
or cache it
or run command on background and cache it (if needed)
If your code takes very long time it will be aborted with timeout error
Slow commands can cause your Cloud to degrade and even crash.
For example, let's say your cloud can process 1 request per second.
Thus, 4 requests of 250 ms each will be processed per second
But if you have slow commands (let's say one second), the cloud will process only one command instead of four.
Thus, the response to the first message will only be sent in a second. And the answer to the last is the fourth already in four.
Caching methods
setCache
Example set command caching for 1 hour (for all bot users):
Example of command caching for 1 hour (for one user only):
clearCache
you can clear Cache (it can be usefull on changing data)
clear for user caching
What can be cached?
Caching is a powerful method for speeding up a bot. But you can't cache everything.
Criteria for caching:
messages from the bot to this command do not change, or rarely change
command accepts no params
(/command any param)
or options(Bot.run(command: "/cmd", options: options))
, or accepts them, but they rarely changethe result of the command is not critical. Even if the old, not updated value is returned, this is not critical
If your command does not meet these requirements try to devide it for several commands. One or more of them can be cached. To run them use methods: Bot.run
or Bot.runCommand
Advanced techniques
You can PRE run caching in background for long command.
Command /start
Command /longBackroundTask
Examples
command /time
:
We have such result (without caching) - take 250 ms:
Edit command for caching:
Now we have such result - take 120 ms (instead 250 ms without caching):
Command result is changed only after 120 seconds. During that 120 seconds answer is same.
Last updated