依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置信息:
spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
该默认配置信息如无特别需求无需更改。
示例:
一下内容仅集成String、List、Hash。且如size()、values()等方法限于篇幅之际不再赘述。
@RestController
public class RedisController {
@Resource
private RedisTemplate redisTemplate;
/**
* @return String
* @author mcry416@outlook.com
*/
@RequestMapping(value = "/redis/string", method = RequestMethod.GET)
public String getRedisReturnOfString(){
// Add key and values into Redis of String type.
redisTemplate.opsForValue().set("account_one", "lucy");
// Get value by opsForValue().get().
String str = (String)redisTemplate.opsForValue().get("account_one");
System.out.println("------> VALUE: " + str);
// Update data by opsForValue().set().
redisTemplate.opsForValue().set("account_one", "mary");
// Delete data by delete() directly.
redisTemplate.delete("account_one");
str = (String)redisTemplate.opsForValue().get("account_one");
return str;
}
/**
* @return List
* @author mcry416@outlook.com
*/
@RequestMapping(value = "/redis/list", method = RequestMethod.GET)
public List getRedisReturnOfList(){
List sList = new ArrayList();
// Left push data by opsForList().leftPush() into List collection.
redisTemplate.opsForList().leftPush("account_list", "test_one");
redisTemplate.opsForList().leftPush("account_list", "test_two");
redisTemplate.opsForList().leftPush("account_list", "test_three");
redisTemplate.opsForList().leftPush("account_list", "test_four");
// Right push data by opsForList().rightPush() into List collection.
redisTemplate.opsForList().rightPush("account_list", "test_1");
redisTemplate.opsForList().rightPush("account_list", "test_2");
// Left pop data by opsForList().leftPop() with a concrete List collection.
redisTemplate.opsForList().leftPop("account_list");
// Right pop data by opsForList().leftPop() with a concrete List collection.
redisTemplate.opsForList().rightPop("account_list");
// Get all data from concrete List collection by opsForList().range().
// And parameter : 0 -1 means all data.
sList.add(redisTemplate.opsForList().range("account_list", 0, -1));
return sList;
}
/**
* @return List<Map></>
* @author mcry416@outlook.com
*/
@RequestMapping(value = "/redis/hash", method = RequestMethod.GET)
public List<Map> getRedisReturnOfHash(){
List<Map> sList = new ArrayList<>();
// Put data into Hash times by opsForHash().put() directly.
redisTemplate.opsForHash().put("hash_key", "account_one", "password_one");
redisTemplate.opsForHash().put("hash_key", "account_two", "password_two");
redisTemplate.opsForHash().put("hash_key", "account_three", "password_three");
Map<String, String> map = new HashMap();
map.put("1","jack");
map.put("2","lucy");
map.put("3","bob");
map.put("4","joe");
// Put all data with a concrete Map collection by opsForHash().putAll().
// First parameter: key
// Second parameter: Map
redisTemplate.opsForHash().putAll("hash_key", map1);
// Get all data with Hash collection into a concrete Map collection.
// And parameter: key
Map<Object, Object> entries = redisTemplate.opsForHash().entries("hash_key");
System.out.println(entries);
sList.add(entries);
return sList;
}
}