依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
|
application.properties
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.database=1
spring.redis.password=root
|
配置类
package com.it.seckill.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; 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.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@EnableCaching public class RedisConfig extends CachingConfigurerSupport {
@Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory){ RedisTemplate<String, Object> template=new RedisTemplate<>(); template.setConnectionFactory(factory); Jackson2JsonRedisSerializer jackson=new Jackson2JsonRedisSerializer(Object.class); ObjectMapper objmap=new ObjectMapper(); objmap.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objmap.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson.setObjectMapper(objmap);
template.setValueSerializer(jackson); template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(jackson); template.afterPropertiesSet(); return template; } }
|
* String redisTemplate.opsForValue()
* hash redisTemplate.opsForhash()
* set redisTemplate.opsForset()
* list redisTemplate.opsForlist()
* sortset redisTemplate.opsForzset()
|
String操作
存取对象
package com.it.seckill.controller;
import com.it.seckill.config.RedisConfig; import com.it.seckill.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.util.Date; import java.util.concurrent.TimeUnit;
@RestController @RequestMapping("/redis2") public class StringController {
@Autowired RedisTemplate redisTemplate;
@RequestMapping("/put") public String put(){ User us=new User(3,"张三",new Date()); redisTemplate.opsForValue().set("reidsz",us); return "putok"; }
@RequestMapping("/get") public Object get(){ User us= (User) redisTemplate.opsForValue().get("reidsz"); return us; }
@RequestMapping("/del") public String delkey(){ redisTemplate.delete("reidsz"); return "delok"; }
@RequestMapping("/tdel") public String timdel(){ redisTemplate.expire("reidsz",60, TimeUnit.SECONDS); return "stringtimedel"; }
}
|
Hash操作
package com.it.seckill.controller;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit;
@RestController @RequestMapping("/hashredis") public class HashController {
@Autowired RedisTemplate redisTemplate;
@RequestMapping("/putha") public String putha(){ redisTemplate.opsForHash().put("hase1","name","渣男"); return "puthash"; }
@RequestMapping("/getha") public String getha(){ String st= (String) redisTemplate.opsForHash().get("hase1","name"); System.out.println(st); return "getha"; }
@RequestMapping("/delha") public String deha(){ redisTemplate.delete("hase1"); return "Delsu"; }
@RequestMapping("/tiemdel") public String time(){ redisTemplate.expire("hase1",30, TimeUnit.SECONDS); return "timede"; } }
|
List操作
package com.it.seckill.controller;
import com.it.seckill.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.concurrent.TimeUnit;
@RestController @RequestMapping("/listredis") public class ListController {
@Autowired RedisTemplate redisTemplate;
@RequestMapping("/putlis") public String pulist(){ User us=new User(1,"张三",new Date()); User us2=new User(2,"张三2",new Date()); List lis=new ArrayList(); lis.add(us); lis.add(us2); redisTemplate.opsForList().leftPushAll("jlist",lis); return "putlist"; }
@RequestMapping("/getlis") public List getlis(){ List lis= redisTemplate.opsForList().range("jlist",0,-1); return lis; }
@RequestMapping("/dellist") public String del(){ redisTemplate.delete("jlist"); return "delsu"; }
@RequestMapping("/timedel") public String timde(){ redisTemplate.expire("jlist",60, TimeUnit.SECONDS); return "tiemel"; } }
|
set操作
package com.it.seckill.controller;
import com.it.seckill.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.util.Date; import java.util.Set; import java.util.concurrent.TimeUnit;
@RestController @RequestMapping("/setredis") public class SetController {
@Autowired RedisTemplate redisTemplate;
@RequestMapping("/putset") public String putset(){ User us=new User(1,"李四",new Date()); redisTemplate.opsForSet().add("se1",us); return "putsu"; }
@RequestMapping("/getset") public String getset(){ Set se=redisTemplate.opsForSet().members("se1"); System.out.println(se); return "getsu"; }
@RequestMapping("/delset") public String delset(){ redisTemplate.delete("se1"); return "deset"; }
@RequestMapping("/dsdel") public String timedel(){ redisTemplate.expire("se1",30, TimeUnit.SECONDS); return "desul"; } }
|
sortset操作
有序set
package com.it.seckill.controller;
import com.it.seckill.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.util.Date; import java.util.Set; import java.util.concurrent.TimeUnit;
@RestController @RequestMapping("/sort") public class SortSetController {
@Autowired RedisTemplate redisTemplate;
@RequestMapping("/putsort") public String putsort(){ redisTemplate.opsForZSet().add("class","c1901",100); return "sortsu"; }
@RequestMapping("/getsort") public String getsort(){ Set cf=redisTemplate.opsForZSet().range("class",0,-1); System.out.println(cf); return "setget"; }
@RequestMapping("/delsort") public String desort(){ redisTemplate.delete("class"); return "delsort"; }
@RequestMapping("/timedel") public String tdel(){ redisTemplate.expire("class",30, TimeUnit.SECONDS); return "tdel"; }
}
|