1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set;
@SpringBootTest(classes = App.class) @RunWith(SpringRunner.class) public class RedisTemplateTest {
@Autowired private RedisTemplate<String, String> redisTemplate;
@Test public void stringTest() { this.redisTemplate.delete("name"); this.redisTemplate.opsForValue().set("name", "xiaoyuge"); String name = this.redisTemplate.opsForValue().get("name"); System.out.println(name); }
@Test public void listTest() { this.redisTemplate.delete("names"); this.redisTemplate.opsForList().rightPushAll("names", "tom", "chelly", "xiaoyuge", "fan"); List<String> list = this.redisTemplate.opsForList().range("names", 0, -1); list.forEach(System.out::println); }
@Test public void setTest() { this.redisTemplate.opsForSet().add("courses", "java", "php", "node", "js"); Set<String> courseSet = this.redisTemplate.opsForSet().members("courses"); courseSet.forEach(System.out::println); }
@Test public void hashTest() { Map<String, String> map = new HashMap<String, String>() {{ put("name", "xiaoyuge"); put("age", "18"); }}; this.redisTemplate.opsForHash().putAll("userMap", map);
Map<Object, Object> resultMap = this.redisTemplate.opsForHash().entries("userMap"); System.out.println(resultMap); }
@Test public void zsetTest() { this.redisTemplate.delete("mv"); this.redisTemplate.opsForZSet().add("mv", "赵丽颖", 960d); this.redisTemplate.opsForZSet().add("mv", "袁冰妍", 98d); this.redisTemplate.opsForZSet().add("mv", "刘亦菲", 100d); this.redisTemplate.opsForZSet().add("mv", "杨超越", 80d);
Set<String> mvs = this.redisTemplate.opsForZSet().range("mv", 0, -1); mvs.forEach(System.out::println); } }
|