编程知识 cdmana.com

Springboot integrates redis

1. Add configuration file :

  1. Redis.properties
#  Configuration of single unit redis The server 
redis.host=192.168.126.129 ip Address 
redis.port=6379    Port number 
  1. Set configuration class :

RedisConfig
image.png

2. Convert objects to Json Format introduction case

API: MAPPER.writeValueAsString(itemDesc);
image.png

image.png

3. Convert objects to Json Format optimization

4.AOP Realization Redis cache

AOP Realization Redis

1.@cacheFind() annotation

Implementation strategy :

A. Need custom annotation CacheFind
B. Set the parameters of the annotation key The prefix of Time out of data
C. Identify the annotation in the method ,
D. utilize aop Block the specified annotation
E. Use surround notification around notice Implement cache business

Mode one
Edit custom annotations :
1.CacheFind
image.png
2. Identify annotations on methods
image.png
3. stay aop It intercepts annotations ( Pointcut expression )_CacheAOP
image.png
Mode two Optimize

1. Problems with existing code

1. If you directly cache business , Write to the business layer , If future cache code changes , Code coupling is high
2. If other businesses need , Must rewrite editing code , Low efficiency

2.AOP Concept :

1.AOP-- Section oriented programming , Through precompiling and runtime A dynamic proxy ; Reduce coupling
Improve the reusability of programs , At the same time, improve the development efficiency ;

3.AOP The specific steps of implementation :

The formula AOP( section )= Notification method + Pointcut expression

Five ways of notification :
1.before

Execute before executing the target method

2.after

Execute after executing the target method

3.afterReturning

When an error is reported after the target method is executed

4.afterThrowing

Whenever the program is finished , Notice to be executed

5.around ___ The most powerful

Surrounding the notification The method is ahead of the goal , After execution
You can control whether the target method is executed or not You can control the trajectory of program execution ;

notes :
front 4 The middle notice can't control whether the target is executed or not , It is generally used to record the execution status of a program ; Generally used in monitoring operations, only for recording

Four pointcut expressions :
bean ("bean Of id")

Particle size : Press Bean matching ( It's the realization of instantiation ) At present bean Methods in will execute the notification ___ coarse-grained

within (" Package name . Class name ") ___ coarse-grained

You can match multiple classes with wildcards *

execution (" return type Package name . Class name . Method name ( parameter list )")

fine-grained Method parameter level

@annotation (" Package name . Class name ")

fine-grained Installation annotation matches

版权声明
本文为[Li Guodong]所创,转载请带上原文链接,感谢

Scroll to Top