使用RedisTemplate封装成工具Bean(spring-data-redis)

  • Post author:
  • Post category:其他


SpringBooy整合Redis:

一、Redis依赖

spring-data-redis 2.3.9

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

在这里插入图片描述

二、封装成springBean–SimpleRedisSercvice

import com.alibaba.nacos.common.utils.CollectionUtils;
import org.slf4j.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;
import org.springframework.util.SerializationUtils;

import java.io.Serializable;
import java.util.Set;

/**
 * @author huang
 * redis的简单实现
 * 使用RedisTemplate中底层实现
 */
@Service
public class SimpleRedisService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public void save(String key, Serializable value) {
        final byte[] keybytes = key.getBytes();
        final byte[] valuebytes = SerializationUtils.serialize(value);
        redisTemplate.execute(new RedisCallback<Object>() {
            @Override
            public Object doInRedis(RedisConnection connection)
                    throws DataAccessException {
                connection.set(keybytes, valuebytes);
                return null;
            }
        });
    }
    public void save(String key, Serializable value, Long seconds) {
        final byte[] keybytes = key.getBytes();
        final byte[] valuebytes = SerializationUtils.serialize(value);
        redisTemplate.execute(new RedisCallback<Object>() {
            @Override
            public Object doInRedis(RedisConnection connection)
                    throws DataAccessException {
                connection.setEx(keybytes, seconds, valuebytes);
                return null;
            }
        });
    }

    public void save2h(String key, Serializable value) {
        final byte[] keybytes = key.getBytes();
        final byte[] valuebytes = SerializationUtils.serialize(value);
        redisTemplate.execute(new RedisCallback<Object>() {
            @Override
            public Object doInRedis(RedisConnection connection)
                    throws DataAccessException {
                connection.set(keybytes, valuebytes);
                connection.expire(keybytes, 7200);
                return null;
            }
        });
        logger.info("SimpleRedis.save2h done");
    }


    public void save(String key, Object value) {
        final byte[] keybytes = key.getBytes();
        final byte[] valuebytes = SerializationUtils.serialize(value);
        redisTemplate.execute(new RedisCallback<Object>() {
            @Override
            public Object doInRedis(RedisConnection connection)
                    throws DataAccessException {
                connection.set(keybytes, valuebytes);
                return null;
            }
        });
        logger.info("SimpleRedis.save done");
    }

    public <T> T find(final String key) {
        try {
            T execute = redisTemplate.execute(new RedisCallback<T>() {
                @Override
                public T doInRedis(RedisConnection connection)
                        throws DataAccessException {
                    byte[] keybytes = redisTemplate.getStringSerializer().serialize(key);
                    if (connection.exists(keybytes)) {
                        byte[] valuebytes = connection.get(keybytes);
                        @SuppressWarnings("unchecked")
                        T value = (T) SerializationUtils.deserialize(valuebytes);
                        return value;
                    }
                    return null;
                }
            });
            return execute;
        } catch (Exception execute) {
            execute.printStackTrace();
            return null;
        }
    }

    public Long delete(final String key) {
        logger.info("SimpleRedis.delete" + key);
        final byte[] keybytes = key.getBytes();
        return (Long) redisTemplate.execute(new RedisCallback<Object>() {
            @Override
            public Long doInRedis(RedisConnection connection)
                    throws DataAccessException {
                return connection.del(keybytes);
            }
        });
    }

    public void deleteByPrefix(String prex) {
        Set<String> keys = redisTemplate.keys(prex);
        if (CollectionUtils.isNotEmpty(keys)) {
            redisTemplate.delete(keys);
        }
    }

    public Boolean exists(final String key) {
        return (Boolean) redisTemplate.execute(new RedisCallback<Object>() {
            @Override
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                byte[] keybytes = redisTemplate.getStringSerializer().serialize(key);
                return connection.exists(keybytes);
            }
        });
    }

}



版权声明:本文为weixin_47162914原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。