Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the concept of SpringBoot and SpringCache

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about what the concepts of SpringBoot and SpringCache are. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The concept of 1.SpringCache

First of all, we know that jpa,jdbc these things are some specifications, such as jdbc, to connect to the database, all need to use database connection, preprocessing, result set of these three objects, whether to connect to mysql or oracle all need to use these three objects, this is a specification, and SpringCache is a cache specification, the specific implementation of redis,EhCahe and so on.

2.SpringCache usage (redis version) 2.1. Basic SpringCache usage

1.pom.xml

4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.3 com.yl cache_redis 0.0.1-SNAPSHOT cache_redis Demo project for Spring Boot 11 org.springframework.boot spring-boot-starter-cache spring-boot-starter-data-redis Spring-boot-starter-web spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin

2.application.properties

# configuration spring.redis.host=192.168.244.135spring.redis.port=6379spring.redis.password=root123 of redis

3. Entity class

Package com.yl.cache_redis.domain;import java.io.Serializable;public class User implements Serializable {private Integer id; private String username; private String password; public Integer getId () {return id;} public void setId (Integer id) {this.id = id;} public String getUsername () {return username;} public void setUsername (String username) {this.username = username } public String getPassword () {return password;} public void setPassword (String password) {this.password = password } @ Override public String toString () {return "User {" + "id=" + id + ", username='" + username +'\'+ ", password='" + password +'\'+'}';}}

4.service

Package com.yl.cache_redis;import com.yl.cache_redis.domain.User;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;@Servicepublic class UserService {@ Cacheable (cacheNames = "U1") / / this annotation stores the returned value of the method in the cache public User getUserById (Integer id) {System.out.println ("getUserById:" + id); User user = new User () User.setId (id); user.setUsername ("root"); user.setPassword ("root"); return user;}}

5. The main program, plus the comments to open the cache

Package com.yl.cache_redis;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cache.annotation.EnableCaching;@SpringBootApplication@EnableCaching / / enable caching function public class CacheRedisApplication {public static void main (String [] args) {SpringApplication.run (CacheRedisApplication.class, args);}}

6. test

6.1When userservice is not annotated with @ Cacheable

6.2After userservice was annotated with @ Cacheable, it was found that the method in sevice was called only once.

You can also see the data in the cache in redis, where key is the parameter of the defined cacheNames+::+ method.

2.2. Spring Cache Custom Cache key

By default, 1.SpringCache uses a combination of cacheNames and parameters in the method to form key, so what if there are multiple parameters? How does it make up key? Can we specify key?

two。 How to customize key?

1) Custom key

Package com.yl.cache_redis;import org.springframework.cache.interceptor.KeyGenerator;import org.springframework.stereotype.Component;import java.lang.reflect.Method;import java.util.Arrays;@Componentpublic class MyKeyGenerator implements KeyGenerator {@ Override public Object generate (Object target, Method method, Object...) Params) {return target.toString () + ":" + method.getName () + ":" + Arrays.toString (params);}}

2) Test

2.3. SpringCache updates cache

1. Use the @ CachePut annotation to update. Note: the key in @ CachePut should be the same as the key in @ Cacheable, otherwise it won't be updated!

2.4. SpringCache clears the cache

1. Using @ CacheEvict annotation, the main key is the same as the key in @ Cacheable

two。 test

2.5. Other uses of SpringCache

1.@Caching comments, which can be combined with multiple annotations

2.@CacheConfig comments

3.SpringCache usage (EhCache version)

1.pom.xml

4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.3 com.yl ehcache 0.0.1-SNAPSHOT ehcache Demo project for Spring Boot 11 org.springframework.boot spring-boot-starter-cache spring-boot-starter-web spring -boot-starter-test test net.sf.ehcache ehcache 2.10.6 org.springframework.boot spring-boot-maven-plugin

two。 Entity class

Package com.yl.ehcache.model;import java.io.Serializable;public class User implements Serializable {private Integer id; private String username; private String password; public Integer getId () {return id;} public void setId (Integer id) {this.id = id;} public String getUsername () {return username;} public void setUsername (String username) {this.username = username } public String getPassword () {return password;} public void setPassword (String password) {this.password = password } @ Override public String toString () {return "User {" + "id=" + id + ", username='" + username +'\'+ ", password='" + password +'\'+'}';}}

3.service

Package com.yl.ehcache.service;import com.yl.ehcache.model.User;import org.springframework.cache.annotation.CacheEvict;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;@Servicepublic class UserService {@ Cacheable (cacheNames = "user") public User getUserById (Integer id) {System.out.println ("getUserById ()..."); User user = new User (); user.setId (id) User.setUsername ("root"); user.setPassword ("root"); return user;} @ CacheEvict (cacheNames = "user") public void delete (Integer id) {System.out.println ("delete");}

4. Main program

Package com.yl.ehcache;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cache.annotation.EnableCaching;@SpringBootApplication@EnableCachingpublic class EhcacheApplication {public static void main (String [] args) {SpringApplication.run (EhcacheApplication.class, args);}}

5.ehcache.xml

6. test

Thank you for reading! This is the end of the article on "what is the concept of SpringBoot and SpringCache". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report