• 大小: 47KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-15
  • 语言: 其他
  • 标签: redis  spring  

资源简介

spring + redis使用@Cacheable,@CachePut,@CacheEvict详细步骤

资源截图

代码片段和文件信息

package com.susq.work;

import lombok.extern.slf4j.Slf4j;
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 java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.objectInputStream;
import java.io.objectOutputStream;

/**
 * @author susq
 * @since 2018-01-05-10:33
 */
@Slf4j
public class MyRedisCache implements Cache {
    private RedisTemplateject> redisTemplate;
    private String name;

    public RedisTemplateject> getRedisTemplate() {
        return redisTemplate;
    }

    public void setRedisTemplate(RedisTemplateject> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public object getNativeCache() {
        return this.redisTemplate;
    }

    @Override
    public ValueWrapper get(object key) {
        System.out.println(“get key“);
        final String keyf = key.toString();
        object object = null;
        object = redisTemplate.execute(new RedisCallbackject>() {
            public object doInRedis(RedisConnection connection) throws DataAccessException {
                byte[] key = keyf.getBytes();
                byte[] value = connection.get(key);
                if (value == null) {
                    return null;
                }
                return toobject(value);
            }
        });
        return (object != null ? new SimpleValueWrapper(object) : null);
    }


    @Override
    public void put(object key object value) {
        System.out.println(“put key“);
        final String keyf = key.toString();
        final object valuef = value;
        final long liveTime = 86400;
        redisTemplate.execute(new RedisCallback() {
            public Long doInRedis(RedisConnection connection)
                    throws DataAccessException {
                byte[] keyb = keyf.getBytes();
                byte[] valueb = toByteArray(valuef);
                connection.set(keyb valueb);
                if (liveTime > 0) {
                    connection.expire(keyb liveTime);
                }
                return 1L;
            }
        });
    }

    private byte[] toByteArray(object obj) {
        byte[] bytes = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            objectOutputStream oos = new objectOutputStream(bos);
            oos.writeobject(obj);
            oos.flush();
            bytes = bos.toByteArray()

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-05-18 22:15  redis-second\
     目录           0  2018-05-18 22:15  redis-second\.idea\
     目录           0  2018-05-17 20:30  redis-second\.idea\artifacts\
     文件         315  2018-05-17 15:28  redis-second\.idea\artifacts\redis_second_war.xml
     文件        3415  2018-05-17 20:30  redis-second\.idea\artifacts\redis_second_war_exploded.xml
     文件         561  2018-05-17 15:18  redis-second\.idea\checkstyle-idea.xml
     文件         636  2018-05-17 15:45  redis-second\.idea\compiler.xml
     文件         172  2018-05-17 15:28  redis-second\.idea\encodings.xml
     目录           0  2018-05-17 15:19  redis-second\.idea\inspectionProfiles\
     目录           0  2018-05-17 20:30  redis-second\.idea\libraries\
     文件         514  2018-05-17 15:28  redis-second\.idea\libraries\Maven__aopalliance_aopalliance_1_0.xml
     文件         448  2018-05-17 15:28  redis-second\.idea\libraries\Maven__cglib_cglib_3_1.xml
     文件         568  2018-05-17 15:28  redis-second\.idea\libraries\Maven__ch_qos_logback_logback_classic_1_1_7.xml
     文件         547  2018-05-17 15:28  redis-second\.idea\libraries\Maven__ch_qos_logback_logback_core_1_1_7.xml
     文件         558  2018-05-17 15:28  redis-second\.idea\libraries\Maven__commons_logging_commons_logging_1_2.xml
     文件         644  2018-05-17 15:28  redis-second\.idea\libraries\Maven__com_fasterxml_jackson_core_jackson_annotations_2_9_0.xml
     文件         595  2018-05-17 15:28  redis-second\.idea\libraries\Maven__com_fasterxml_jackson_core_jackson_core_2_9_3.xml
     文件         623  2018-05-17 15:28  redis-second\.idea\libraries\Maven__com_fasterxml_jackson_core_jackson_databind_2_9_3.xml
     文件         578  2018-05-17 15:28  redis-second\.idea\libraries\Maven__javax_servlet_javax_servlet_api_3_1_0.xml
     文件         455  2018-05-17 15:28  redis-second\.idea\libraries\Maven__junit_junit_4_12.xml
     文件         570  2018-05-17 15:28  redis-second\.idea\libraries\Maven__org_apache_commons_commons_pool2_2_4_2.xml
     文件         532  2018-05-17 15:28  redis-second\.idea\libraries\Maven__org_hamcrest_hamcrest_core_1_3.xml
     文件         500  2018-05-17 19:53  redis-second\.idea\libraries\Maven__org_mybatis_mybatis_3_2_5.xml
     文件         549  2018-05-17 19:53  redis-second\.idea\libraries\Maven__org_mybatis_mybatis_spring_1_2_2.xml
     文件         458  2018-05-17 15:28  redis-second\.idea\libraries\Maven__org_ow2_asm_asm_4_2.xml
     文件         524  2018-05-17 15:28  redis-second\.idea\libraries\Maven__org_projectlombok_lombok_1_16_8.xml
     文件         513  2018-05-17 20:30  redis-second\.idea\libraries\Maven__org_slf4j_slf4j_api_1_7_21.xml
     文件         678  2018-05-17 15:28  redis-second\.idea\libraries\Maven__org_springframework_data_spring_data_redis_1_6_0_RELEASE.xml
     文件         609  2018-05-17 15:28  redis-second\.idea\libraries\Maven__org_springframework_spring_aop_4_2_5_RELEASE.xml
     文件         623  2018-05-17 15:28  redis-second\.idea\libraries\Maven__org_springframework_spring_beans_4_2_5_RELEASE.xml
     文件         637  2018-05-17 15:28  redis-second\.idea\libraries\Maven__org_springframework_spring_context_4_2_5_RELEASE.xml
............此处省略76个文件信息

评论

共有 条评论