In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the example analysis of SpringBoot log annotations and cache optimization. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Log comments:
This time, the log output is achieved by annotating + Aop.
First, you need to define an annotation class:
@ Target (ElementType.METHOD) @ Retention (RetentionPolicy.RUNTIME) @ Documented public @ interface LogAnnotation {String module () default "; / / module name String operation () default"; / / Operation name}
Then define the tangent point:
/ / define the pointcut @ Pointcut ("@ annotation (com.xbhog.springbootvueblog.common.aop.LogAnnotation)") public void logPointCut () {}
In vernacular, wherever the notes go, they are cut-off points; if you have a more professional explanation, you can use Baidu or Google.
With the pointcut, then we need to implement the notification event, which uses surround notification, that is, it will be enhanced before and after.
/ / before and after circling the processing flow @ Around ("logPointCut ()") public Object around (ProceedingJoinPoint point) throws Throwable {long beginTime = System.currentTimeMillis (); / / execution method Object result = point.proceed (); / / execution duration (Ms) long time = System.currentTimeMillis ()-beginTime; / / Save log recordLog (point, time); return result;}
Point.proceed () is the method that needs to be executed under the annotation; similar to the following code snippet:
@ LogAnnotation (module = "listArticle", operation = "Show home page display data") public Result listArticle (@ RequestBody PageParams pageParams) {return articleService.listArticle (pageParams);}
Then we need to set up the log output information (recordLog), where we use reflection to get the corresponding class and method names and other information.
Private void recordLog (ProceedingJoinPoint joinPoint, long time) {MethodSignature signature = (MethodSignature) joinPoint.getSignature (); Method method = signature.getMethod (); LogAnnotation logAnnotation = method.getAnnotation (LogAnnotation.class); log.info ("= log start=="); log.info ("module: {}", logAnnotation.module ()); log.info ("operation: {}", logAnnotation.operation ()) / / request method name String className = joinPoint.getTarget (). GetClass (). GetName (); String methodName = signature.getName (); log.info ("request method: {}", className + "." + methodName + "()"); / / request parameter Object [] args = joinPoint.getArgs (); String params = JSON.toJSONString (args [0]); log.info ("params: {}", params) / / get request setting IP address HttpServletRequest request = HttpContextUtils.getHttpServletRequest (); log.info ("ip: {}", IpUtils.getIpAddr (request)); log.info ("excute time: {} ms", time); log.info ("= log end==");}
In this way, the tangent point and section have been completed, and when you use it, you only need to annotate the method directly to obtain the log information of the corresponding method, so that you can locate the error report when you go online.
Cache optimization:
This is implemented in a specific project, the process is generally similar to the above implementation, and is also implemented using annotations.
Why do you need to use cache to improve the efficiency of accessing web page content? because the reading speed of memory is much faster than that of hard disk, this is a better experience for users, but not all data has to be put into cache. because memory is much more expensive than disk, it also takes some experience to cache which data can make the user and the server balanced.
Cache Note:
@ Target ({ElementType.METHOD}) @ Retention (RetentionPolicy.RUNTIME) @ Documented public @ interface Cache {long expire () default 1 * 60 * 1000; / / time to live String name () default ""; / / name of the cache}
The survival time and name of the data are set so that the data can be read in memory within a certain period of time.
Next, take a look at the cached AOP implementation:
Let's take a look at the processing flow around the notification:
According to the illustration, first get the class name and the called method name, and then set up two arrays, one to save the parameter type and the other to save the parameter.
Traverse the parameter, convert it to a string, and then determine whether the string parameter (params) is empty or not, encrypt the current string parameter and save the current encrypted password to Redis. Each time you enter the section, you need to determine whether the RedisValue is empty. If it is empty, you need to perform comments.
The following method, if not empty, reads the data directly from the Redis and displays it on the foreground.
Through the functional operations of log Aop and cache Aop, the results are as follows:
This is the end of the article on "sample analysis of SpringBoot log annotations and cache optimization". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.