The route function uses a CachePlugin that declares a cache time of 10 seconds and enables the cache name to include the request parameters.
Also, only the key1 parameter in the route function uses the CacheRespExtraParam expansion parameter,
so that the CachePlugin will only take the parameter that uses the CacheRespExtraParam parameter, not all of them.
After running the code and executing the curl command,
can see that the route function returns the same content when the request parameters are the same:
In addition to the cache_time and enable_cache_name_merge_param parameters, CachePlugin supports other parameters, as described below:
redis: Specify the Redis instance used by the cache plugin, it is recommended to specify the Redis instance via the CacheResponsePlugin.set_redis_to_app method.
name: Specify the cache Key of the route function, if this value is null, the cache Key is the name of the route function.
enable_cache_name_merge_param: If True, the construction of the cached Key will include other parameter values, such as the following route function.
When the request url carries ?uid=10086&name=so1n, the cache plugin generates a cache Key of demo:10086:so1n.
However, if the parameter uid uses the CacheRespExtraParam expansion parameter, then the cached Key will only include the value of the parameter that uses the CacheRespExtraParam expansion parameter, such as the following route function:
When the request url carries ?uid=10086&name=so1n, the cache plugin generates a cache Key of demo:10086.
include_exc: Receive a Tuple that can be exception, if the error thrown by the route function belongs to one of the errors in the Tuple, the exception will be cached, otherwise the exception will be thrown.
cache_time: cache time in seconds.
timeout: To prevent cache conflicts in highly concurrent scenarios, the cache plugin uses Reids locks to prevent resource contention. timeout represents the maximum time the lock can be held.
sleep: When a lock is found to be held by another request, the current request will sleep for a specified amount of time before attempting to acquire the lock, and so on until it acquires the corresponding lock or times out.
blocking_timeout: the maximum time to try to acquire the lock, if None, it will wait forever.