springboot自定义缓存工具类

缓存是高并发系统的利器,可以大大减轻数据库压力,提高响应速度。

以下介绍在spring boot中使用redis来自定义缓存工具类

引入redis pom

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>

缓存服务接口

package com.test.house.service;

import java.util.concurrent.TimeUnit;

/**
 * 自定义缓存工具类
 */
public interface CacheService {

    /**
     * 设置缓存
     * @param key           缓存key
     * @param value         缓存值
     * @param timeout       缓存过期时间
     * @param timeUnit      缓存过期时间单位
     */
    public void setCache(String key, String value, long timeout, TimeUnit timeUnit);

    /**
     * 获取缓存
     * @param key 缓存key
     * @return
     */
    public String getCache(String key);

    public <V,K> String getCache(K key,Closure<V,K> closure);

    public <V,K> String getCache(K key,Closure<V,K> closure,long timeout,TimeUnit timeUnit);

    /**
     * 删除缓存
     * @param key
     */
    public void deleteCache(String key);

}

缓存服务实现

package com.test.house.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class CacheServiceImpl implements CacheService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private long timeout = 1L;

    private TimeUnit timeUnit = TimeUnit.HOURS;

    @Override
    public void setCache(String key, String value, long timeout, TimeUnit timeUnit) {
        stringRedisTemplate.opsForValue().set(key,value,timeout,timeUnit);
    }

    @Override
    public String getCache(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    @Override
    public <V, K> String getCache(K key, Closure<V, K> closure) {
        return doGetCache(key,closure,this.timeout,this.timeUnit);
    }

    @Override
    public <V, K> String getCache(K key, Closure<V, K> closure, long timeout, TimeUnit timeUnit) {
        return doGetCache(key,closure,timeout,timeUnit);
    }

    @Override
    public void deleteCache(String key) {
        stringRedisTemplate.delete(key);
    }

    private <K,V> String doGetCache(K key,Closure<V,K> closure,long timeout,TimeUnit timeUnit){
        String ret = getCache(key.toString());
        if(ret == null){
            Object r = closure.execute(key);
            setCache(key.toString(),r.toString(),timeout,timeUnit);
            return r.toString();
        }
        return ret;
    }
}
package com.test.house.service;

public interface Closure<O,I> {
    public O execute(I input);
}

测试调用

package com.test.house.controller;

import com.test.house.service.CacheService;
import com.test.house.service.Closure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/house")
@RestController
public class HelloController {

    @Autowired
    private CacheService cacheService;

    @RequestMapping("/getCache")
    public String getCache(){
        String cacheKey = "1001";
        return cacheService.getCache(cacheKey, new Closure<String, String>() {

            @Override
            public String execute(String id) {
                //执行你的业务逻辑
                //userService.getById(id)
                return "getCache";
            }
        });
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值