今天看了redis, 只知道redis能做3件事:
1).安装redis
在opt目录下,使用root用户
cd /optmkdir rediswget http://download.redis.io/releases/redis-3.2.8.tar.gztar -zxvf redis-3.2.8.tar.gz cd redis-3.2.8/makecd /optchmod 777 -R redis
2).测试安装redis是否成功
重新开一个终端
3).在原有项目中引入redis,先在pom.xml添加redis的依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.5.RELEASE</version> </dependency>
4).在application.properties中添加redis配置信息
#redis 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
5).新建一个config包,用来存放一些配置文件,新建RedisConfig.java
package com.hsp.config;import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.CachingConfigurerSupport;import org.springframework.cache.annotation.EnableCaching;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;@Configuration @EnableCachingpublic 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; }}
6).在service包中建立一个RedisService.java类
package com.hsp.sercice;public interface RedisService { public void set(String key, Object value); public Object get(String key); }
7).RedisServiceImpl.java
package com.hsp.service.impl;import javax.annotation.Resource;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.stereotype.Service;import com.hsp.repository.UserRepository;import com.hsp.sercice.RedisService;@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); }}
8).UserController.java
package com.hsp.controllerimport org.springframework.beans.factory.annotation.Autowiredimport org.springframework.data.domain.Pageimport org.springframework.data.domain.Pageableimport org.springframework.stereotype.Controllerimport org.springframework.ui.Modelimport org.springframework.web.bind.annotation.GetMappingimport org.springframework.web.bind.annotation.RequestMappingimport org.springframework.web.bind.annotation.RequestMethodimport org.springframework.web.bind.annotation.RequestParamimport org.springframework.web.bind.annotation.ResponseBodyimport com.hsp.entity.Userimport com.hsp.sercice.RedisServiceimport com.hsp.sercice.UserService@Controller@RequestMapping(path="/user")public class UserController { @Autowired private UserService userService @Autowired private RedisService redisService //从redis获取某个用户 @RequestMapping(value = "/getuserfromredis", method = RequestMethod.GET) public @ResponseBody User getRedis(@RequestParam String key) { return (User)redisService.get(key) } //获取所有用户 @RequestMapping(value = "/getusers", method = RequestMethod.GET) public @ResponseBody Page<User> list(Model model, Pageable pageable){ return userService.findAll(pageable) } //添加用户 @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" }}
9).测试
10).这里有一点要注意,需要把存放在redis中的对象序列化,User.java
package com.hsp.entity;import java.io.Serializable;import java.util.Set;import javax.persistence.CascadeType;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.OneToMany;import javax.persistence.Table;@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; 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; } public String getDictum() { return dictum; } public void setDictum(String dictum) { this.dictum = dictum; } public Set<Photo> getSetPhoto() { return setPhoto; } public void setSetPhoto(Set<Photo> setPhoto) { this.setPhoto = setPhoto; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + ", dictum=" + dictum + ", setPhoto=" + setPhoto + "]"; }}