SpringBoot集成Redis示例

依赖:

<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;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值