Introduction to Redis
Redis (official website: https://redis.io) is a memory-based log-type persistent cache database, saved in key-value format. Redis is completely free and open source. It is written in ANSI C language. Like other key-value cache products, Redis has the following three features.
• Redis supports persistence of data, which can save data in memory on disk and can be loaded again for use during restart;
• Redis not only supports simple key-value type data, but also provides storage of data structures such as strings, linked lists, collections, ordered collections and hashs;
• Redis supports data backup, that is, data backup in master-slave mode.
On a Mac system, you don't need to download Redis to use it. Here are the relevant commands to download Redis's compressed packages from Redis's hosting server and unzip them.
wget http://download.redis.io/releases/redis-4.0.8.tar.gztar xzf redis-4.0.8.tar.gzcd redis-4.0.8make
Before using the services provided by Redis, you need to start Redis-related services first. The command to start Redis on the mac system is as follows.
src/redis-server
Then, reopen a Redis client and use the following command to connect to Redis server.
src/redis-cliredis> set foo barOKredis> get foo "bar"
Integrate Redis database
Before using Redis, you need to introduce related dependencies. The scripts that depend on Maven method are as follows:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>
After that, we write the relevant configuration of Redis to yml. Here we recommend writing different configurations according to different environments before. The default port used by Redis is 6379. Usually, Redis uses a database No. 0 by default, and there are 16 databases by default:
#redis configuration redis:# Database index database: 0# Server address host: 127.0.0.1# Server connection port port: 6379# Link password password:# Link pool pool:# Maximum number of connections (negative value means no limit) max-active: 8# Maximum blocking waiting time (negative value means no limit) max-wait: 1# Maximum idle link max-idle: 8# Minimum idle link min-idle: 0# Link timeout (milliseconds) timeout: 0
If it is the application.properties method, some configurations are as follows:
spring.redis.hostName=127.0.0.1spring.redis.port=6379 spring.redis.pool.maxActive=8 spring.redis.pool.maxWait=-1 spring.redis.pool.maxIdle=8 spring.redis.pool.minIdle=0 spring.redis.timeout=0
Create a new RedisConfig.java file to store configuration files.
@Configuration @EnableCaching//Open the annotation public class RedisConfig extends CachingConfigurerSupport { @Bean public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate) { CacheManager cacheManager = new RedisCacheManager(redisTemplate); return cacheManager; } @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>(); redisTemplate.setConnectionFactory(factory); return redisTemplate; }}Create a RedisService.java class in the service package.
public interface RedisService { public void set(String key, Object value); public Object get(String key); }Create a new service implementation class RedisServiceImpl.java.
@Servicepublic class RedisServiceImpl implements RedisService { @Resource private RedisTemplate<String,Object> redisTemplate; public void set(String key, Object value) { ValueOperations<String,Object> vo = redisTemplate.opsForValue(); vo.set(key, value); } public Object get(String key) { ValueOperations<String,Object> vo = redisTemplate.opsForValue(); return vo.get(key); }}Create a new Controller layer code UserController.java
@Controller@RequestMapping(path="/user")public class UserController { @Autowired private UserService userService; @Autowired private RedisService redisService; //Get a user from redis@RequestMapping(value = "/getuserfromredis", method = RequestMethod.GET) public @ResponseBody User getRedis(@RequestParam String key) { return (User)redisService.get(key); } //Get all users @RequestMapping(value = "/getusers", method = RequestMethod.GET) public @ResponseBody Page<User> list(Model model, Pageable pageable){ return userService.findAll(pageable); } //Add user @GetMapping(value="/adduser") public @ResponseBody String addUser(@RequestParam String dictum, @RequestParam String password, @RequestParam String username) { User user = new User(); user.setDictum(dictum); user.setPassword(password); user.setUsername(username); System.out.println(user); userService.saveUser(user); redisService.set(user.getId()+"", user); return "Saved"; }}The code of the entity class User.java designed in this article is as follows: storing objects in redis requires serialization.
@Entity@Table(name="s_user")public class User implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationType.AUTO) private Integer id; private String username; private String password; private String dictum; @OneToMany(mappedBy = "user", fetch = FetchType. LAZY, cascade = {CascadeType. ALL}) private Set<Photo> setPhoto; //Omit getter and setter @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + ", dictum=" + dictum + ", setPhoto=" + setPhoto + "]"; }}Summarize
The above is the method of SpringBoot integrating Redis database introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!