spring使用redis

依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

yml文件配置

spring:
    redis:
    database: 0
    host: 127.0.0.1
    port: 6379

配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory);

        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }
}

redis对字符串数据的通用操作类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * redis对字符串数据的通用操作类
 */
@Component
public class RedisMapper {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 根据键保存字符串
     *
     * @param key
     * @param val
     * @return
     */
    public boolean setKey(String key, String val) {
        redisTemplate.opsForValue().set(key, val);
        if (redisTemplate.hasKey(key)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据键删除
     *
     * @param key
     * @return
     */
    public boolean delKey(String key) {
        if (redisTemplate.delete(key)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 添加并设置过期时间
     *
     * @param key
     * @param val
     * @param timeout
     * @param unit
     * @return
     */
    public boolean setKey(String key, String val, long timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, val, timeout, unit);
        if (redisTemplate.hasKey(key)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据键获得值
     *
     * @param key
     * @return
     */
    public Object getKey(String key) {
        if (redisTemplate.hasKey(key)) {
            Object value = redisTemplate.opsForValue().get(key);
            return value;
        } else {
            return null;
        }
    }

    /**
     * 保存hash数据到redis
     *
     * @param key1  大键
     * @param key2  小键
     * @param value 值,如果是对象使用json字符串
     * @return
     */
    public boolean setHash(String key1, String key2, String value) {
        try {
            redisTemplate.opsForHash().put(key1, key2, value);
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    /**
     * 取出reids中的hash数据
     *
     * @param key1 大键
     * @param key2 小键
     * @return
     */
    public String getHash(String key1, String key2) {
        String result = null;
        try {
            result = redisTemplate.opsForHash().get(key1, key2).toString();
            return result;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据大键取出hash中所有的数据
     *
     * @param key
     * @return
     */
    public Map<Object, Object> getHashEntries(String key) {
        Map<Object, Object> map = redisTemplate.opsForHash().entries(key);
        if (map != null) {
            return map;
        }
        return null;
    }

    /**
     * 判断指定的key是否存在
     *
     * @param key
     * @return
     */
    public Boolean hasKey(String key) {
        try {
            if (redisTemplate.hasKey(key)) {
                return true;
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }


    /**
     * 删除has中指定的键
     *
     * @param key1 大键
     *@param key2  小键
     * @return
     */
    public boolean delHasKey(String key1, String key2) {
        try {
            Long n = redisTemplate.opsForHash().delete(key1, key2);
            if (n > 0) {
                return true;
            }else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值