Cache
Integrated cache module
The WebMVC module has integrated the cache module. The cache of the controller method can be easily implemented through the @Cacheable annotation. The APPLICATION and SESSION scopes can be supported by configuring the scope_processor_class parameter of the cache module. ;
# 设置缓存作用域处理器 ymp.configs.cache.scope_processor_class=net.ymate.platform.webmvc.support.WebCacheScopeProcessor
Sample code: Cache method execution results at the session (SESSION) level for 180 seconds;
@Controller @RequestMapping("/demo") @Cacheable public class CacheController { @RequestMapping("/cache") @Cacheable(scope = ICaches.Scope.SESSION, timeout = 180) public IView doCacheable(@RequestParam String content) throws Exception { // ...... return View.textView("Content: " + content); } }
Note: The method cache based on @Cacheable only caches the result object returned by the controller method, and cannot cache the final execution result of the IView view;
Custom cache processor
The WebMVC module provides the cache processor IWebCacheProcessor interface, which allows developers to perform final processing of the controller execution results through this interface. This interface acts on the Declare the @ResponseCache annotation on the controller class and method;
Description: The framework provides the default implementation of the IWebCacheProcessor interface
net.ymate.platform.webmvc.support.WebCacheProcessor
Used to cache the view execution results,
But it should be noted that when using it, please check the filterDispatchFilter
of web.xml and do not configure it<dispatcher>INCLUDE</ dispatcher>
, otherwise an infinite loop will occur;
@ResponseCache annotation parameter description:
cacheName: cache name, optional parameters, the default value is default ;
key: Cache Key, optional parameter, if not set, it will be automatically generated by the IWebCacheProcessor interface;
processorClass: Custom view cache processor, optional parameter, if not provided, then Use the default IWebCacheProcessor interface parameter configuration;
scope: cache scope, optional parameters, optional values are APPLICATION, SESSION and DEFAULT, the default is DEFAULT;
timeout: cache data timeout, Optional parameter, the value must be greater than or equal to 0, 0 means the default cache is 300 seconds;
useGZip: whether to use GZIP compression, the default value is true
Default IWebCacheProcessor interface parameter configuration:
# 缓存处理器,可选参数 ymp.configs.webmvc.cache_processor_class=demo.WebCacheProc
The framework provides the implementation class of this interface by default:
net.ymate.platform.webmvc.support.WebCacheProcessor
- Based on the Cache caching module, it supports browser-related configurations such as Last-Modified for the Scope.DEFAULT scope in the @ResponseCache annotation, and supports GZIP compression and other features
Sample code:
package demo; import net.ymate.platform.webmvc.*; import net.ymate.platform.webmvc.view.IView; public class WebCacheProc implements IWebCacheProcessor { public boolean processResponseCache(IWebMvc owner, ResponseCache responseCache, IRequestContext requestContext, IView resultView) throws Exception { // 这里是对View视图自定义处理逻辑... // 完整的示例代码请查看net.ymate.platform.webmvc.support.WebCacheProcessor类源码 return false; } } @Controller @RequestMapping("/demo") public class CacheController { @RequestMapping("/cache") @ResponseCache public IView doCacheable(@RequestParam String content) throws Exception { // ...... return View.textView("Content: " + content); } }
Description: This interface method returns a Boolean value to notify the WebMVC framework whether to continue Handling controller views;