缓存类实现Cache接口

package com.oracle.ssm.shop.cache;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.SerializationUtils;

import java.io.Serializable;
import java.util.concurrent.Callable;
import java.util.concurrent.locks.ReadWriteLock;

@Component
public class RedisCache implements Cache {

    @Autowired
    private RedisTemplate redisTemplate;


    //缓存的名字  该缓存类的唯一标识
    private String name;

    /**
     * @Description: 清空缓存
     * @Author:      Administrator
     * @Param:       []
     * @Return       void
     **/
    @Override
    public void clear() {
        System.out.println("-------clear cache-----");
        redisTemplate.execute(new RedisCallback<String>() {
            @Override
            public String doInRedis(RedisConnection connection) throws DataAccessException {
                connection.flushDb();
                return "ok";
            }
        });
    }
    /**
     * @Description: 删除缓存
     * @Author:      Administrator
     * @Param:       [key]
     * @Return       void
     **/
    @Override
    public void evict(Object key) {
        System.out.println("-------remove cache------");
        final String keyf=key.toString();
        redisTemplate.execute(new RedisCallback<Long>() {
            @Override
            public Long doInRedis(RedisConnection connection) throws DataAccessException {
                return connection.del(keyf.getBytes());
            }

        });

    }
    /**
     * @Description: 命中缓存 取缓存
     * @Author:      Administrator
     * @Param:       [key]
     * @Return       org.springframework.cache.Cache.ValueWrapper
     **/
    @Override
    public ValueWrapper get(Object key) {
        System.out.println("------get cache-------"+key.toString());
        final String keyf = key.toString();
        Object object = null;
        object = redisTemplate.execute(new RedisCallback<Object>() {
            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                byte[] key = keyf.getBytes();
                byte[] value = connection.get(key);
                if (value == null) {
                    System.out.println("------hit no cache-------");
                    return null;
                }
                return
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值