spring boot + redis --- 心得
阅读原文时间:2023年07月13日阅读:1

1.前言

  习惯使用springMVC 配置 redis ,现在使用spring boot ,得好好总结怎么在spring boot 配置和使用 ,区别真的挺大的。

2.环境

spring boot  2.1.6.RELEASE

Redis 3.2.100 -win64

jdk 1.8.0_221

3.操作

(1)目录结构

(2)pom.xml


http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.springframework.boot spring-boot-starter-parent 2.1.6.RELEASE
com.example
provider-redis-8003
0.0.1-SNAPSHOT
provider-redis-8003
Demo project for Spring Boot

<properties>  
    <java.version>1.8</java.version>  
</properties>

<dependencies>  
    <!--redis依赖-->  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-data-redis</artifactId>  
    </dependency>

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

    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-test</artifactId>  
        <scope>test</scope>  
        <exclusions>  
            <exclusion>  
                <groupId>org.junit.vintage</groupId>  
                <artifactId>junit-vintage-engine</artifactId>  
            </exclusion>  
        </exclusions>  
    </dependency>

    <dependency>  
        <groupId>org.apache.commons</groupId>  
        <artifactId>commons-lang3</artifactId>  
        <version>3.9</version>  
    </dependency>  
</dependencies>

<build>  
    <plugins>  
        <plugin>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-maven-plugin</artifactId>  
        </plugin>  
    </plugins>  
</build>

(3) application.properties

spring.application.name=provider-redis-8003
server.port=8003

#redis配置
#Redis服务器地址
spring.redis.host=127.0.0.1
#Redis服务器连接端口
spring.redis.port=6379
#Redis数据库索引(默认为0)
spring.redis.database=0

Redis服务器连接密码(默认为空)

spring.redis.password=666666
#连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=600
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=3000
#连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=300
#连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=2
#连接超时时间(毫秒)
spring.redis.timeout=10000

(4)redis配置类

package com.example.providerredis8003.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
* redis配置类
*/
//标记为配置类
@Configuration
//开启缓存
@EnableCaching
public class RedisConfig {

@Bean  
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {

    /\*\*  
     \*  RedisTemplate 相关配置  
     \*/  
    RedisTemplate<String, Object> template = new RedisTemplate<>();  
    // 配置连接工厂  
    template.setConnectionFactory(factory);

    //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)  
    Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);

    ObjectMapper om = new ObjectMapper();  
    // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public  
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);  
    // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常  
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON\_FINAL);  
    jacksonSeial.setObjectMapper(om);

    // 值采用json序列化  
    template.setValueSerializer(jacksonSeial);  
    //使用StringRedisSerializer来序列化和反序列化redis的key值  
    template.setKeySerializer(new StringRedisSerializer());

    // 设置hash key 和value序列化模式  
    template.setHashKeySerializer(new StringRedisSerializer());  
    template.setHashValueSerializer(jacksonSeial);  
    template.afterPropertiesSet();

    return template;  
}

/\*\*  
 \* 对hash类型的数据操作  
 \*  
 \* @param redisTemplate  
 \* @return  
 \*/  
@Bean  
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {  
    return redisTemplate.opsForHash();  
}  
/\*\*  
 \* 对redis字符串类型数据操作  
 \*  
 \* @param redisTemplate  
 \* @return  
 \*/  
@Bean  
public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {  
    return redisTemplate.opsForValue();  
}

/\*\*  
 \* 对链表类型的数据操作  
 \*  
 \* @param redisTemplate  
 \* @return  
 \*/  
@Bean  
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {  
    return redisTemplate.opsForList();  
}

/\*\*  
 \* 对无序集合类型的数据操作  
 \*  
 \* @param redisTemplate  
 \* @return  
 \*/  
@Bean  
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {  
    return redisTemplate.opsForSet();  
}

/\*\*  
 \* 对有序集合类型的数据操作  
 \*  
 \* @param redisTemplate  
 \* @return  
 \*/  
@Bean  
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {  
    return redisTemplate.opsForZSet();  
}  

}

(5)封装好的部分redis工具 ,还需要什么可以自己加

package com.example.providerredis8003.util;

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

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

/**
* redisTemplate封装
*
* @author zjjlive@dist.com.cn
*/
@Component
public class RedisUtil {
/*
string 95行
hash 168行
list 397
set 303
sorct set

 \*/

@Autowired  
private RedisTemplate<String, Object> redisTemplate;

public RedisUtil(RedisTemplate<String, Object> redisTemplate) {  
    this.redisTemplate = redisTemplate;  
}

/\*\*  
 \* 指定缓存失效时间  
 \*  
 \* @param key  键  
 \* @param time 时间(秒)  
 \* @return  
 \*/  
public boolean expire(String key, long time) {  
    try {  
        if (time > 0) {  
            redisTemplate.expire(key, time, TimeUnit.SECONDS);  
        }  
        return true;  
    } catch (Exception e) {  
        e.printStackTrace();  
        return false;  
    }  
}

/\*\*  
 \* 根据key 获取过期时间  
 \*  
 \* @param key 键 不能为null  
 \* @return 时间(秒) 返回0代表为永久有效  
 \*/  
public long getExpire(String key) {  
    return redisTemplate.getExpire(key, TimeUnit.SECONDS);  
}

/\*\*  
 \* 判断key是否存在  
 \*  
 \* @param key 键  
 \* @return true 存在 false不存在  
 \*/  
public boolean hasKey(String key) {  
    try {  
        return redisTemplate.hasKey(key);  
    } catch (Exception e) {  
        e.printStackTrace();  
        return false;  
    }  
}

/\*\*  
 \* 删除缓存  
 \*  
 \* @param key 可以传一个值 或多个  
 \*/  
@SuppressWarnings("unchecked")  
public void del(String... key) {  
    if (key != null && key.length > 0) {  
        if (key.length == 1) {  
            redisTemplate.delete(key\[0\]);  
        } else {  
            redisTemplate.delete(CollectionUtils.arrayToList(key));  
        }  
    }  
}

/\*\*  
 \* 查找所有匹配给定的模式的键  
 \*/  
public Set<String> keys(String pattern){  
  return   redisTemplate.keys(pattern);  
}

//============================String=============================

/\*\*  
 \* 普通缓存获取  
 \*  
 \* @param key 键  
 \* @return 值  
 \*/  
public Object get(String key) {  
    return key == null ? null : redisTemplate.opsForValue().get(key);  
}

/\*\*  
 \* 普通缓存放入  
 \*  
 \* @param key   键  
 \* @param value 值  
 \* @return true成功 false失败  
 \*/  
public boolean set(String key, Object value) {  
    try {  
        redisTemplate.opsForValue().set(key, value);  
        return true;  
    } catch (Exception e) {  
        e.printStackTrace();  
        return false;  
    }  
}

/\*\*  
 \* 普通缓存放入并设置时间  
 \*  
 \* @param key   键  
 \* @param value 值  
 \* @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期  
 \* @return true成功 false 失败  
 \*/  
public boolean set(String key, Object value, long time) {  
    try {  
        if (time > 0) {  
            redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);  
        } else {  
            set(key, value);  
        }  
        return true;  
    } catch (Exception e) {  
        e.printStackTrace();  
        return false;  
    }  
}

/\*\*  
 \* 递增  
 \*  
 \* @param key   键  
 \* @param delta 要增加几(大于0)  
 \* @return  
 \*/  
public long incr(String key, long delta) {  
    if (delta < 0) {  
        throw new RuntimeException("递增因子必须大于0");  
    }  
    return redisTemplate.opsForValue().increment(key, delta);  
}

/\*\*  
 \* 递减  
 \*  
 \* @param key   键  
 \* @param delta 要减少几(小于0)  
 \* @return  
 \*/  
public long decr(String key, long delta) {  
    if (delta < 0) {  
        throw new RuntimeException("递减因子必须大于0");  
    }  
    return redisTemplate.opsForValue().increment(key, -delta);  
}

//================================Map=================================

/\*\*  
 \* HashGet  
 \*  
 \* @param key  键 不能为null  
 \* @param item 项 不能为null  
 \* @return 值  
 \*/  
public Object hget(String key, String item) {  
    return redisTemplate.opsForHash().get(key, item);  
}

/\*\*  
 \* 获取hashKey对应的所有键值  
 \*  
 \* @param key 键  
 \* @return 对应的多个键值  
 \*/  
public Map<Object, Object> hmget(String key) {  
    return redisTemplate.opsForHash().entries(key);  
}

/\*\*  
 \* HashSet  
 \*  
 \* @param key 键  
 \* @param map 对应多个键值  
 \* @return true 成功 false 失败  
 \*/  
public boolean hmset(String key, Map<String, Object> map) {  
    try {  
        redisTemplate.opsForHash().putAll(key, map);  
        return true;  
    } catch (Exception e) {  
        e.printStackTrace();  
        return false;  
    }  
}

/\*\*  
 \* HashSet 并设置时间  
 \*  
 \* @param key  键  
 \* @param map  对应多个键值  
 \* @param time 时间(秒)  
 \* @return true成功 false失败  
 \*/  
public boolean hmset(String key, Map<String, Object> map, long time) {  
    try {  
        redisTemplate.opsForHash().putAll(key, map);  
        if (time > 0) {  
            expire(key, time);  
        }  
        return true;  
    } catch (Exception e) {  
        e.printStackTrace();  
        return false;  
    }  
}

/\*\*  
 \* 向一张hash表中放入数据,如果不存在将创建  
 \*  
 \* @param key   键  
 \* @param item  项  
 \* @param value 值  
 \* @return true 成功 false失败  
 \*/  
public boolean hset(String key, String item, Object value) {  
    try {  
        redisTemplate.opsForHash().put(key, item, value);  
        return true;  
    } catch (Exception e) {  
        e.printStackTrace();  
        return false;  
    }  
}

/\*\*  
 \* 向一张hash表中放入数据,如果不存在将创建  
 \*  
 \* @param key   键  
 \* @param item  项  
 \* @param value 值  
 \* @param time  时间(秒)  注意:如果已存在的hash表有时间,这里将会替换原有的时间  
 \* @return true 成功 false失败  
 \*/  
public boolean hset(String key, String item, Object value, long time) {  
    try {  
        redisTemplate.opsForHash().put(key, item, value);  
        if (time > 0) {  
            expire(key, time);  
        }  
        return true;  
    } catch (Exception e) {  
        e.printStackTrace();  
        return false;  
    }  
}

/\*\*  
 \* 删除hash表中的值  
 \*  
 \* @param key  键 不能为null  
 \* @param item 项 可以使多个 不能为null  
 \*/  
public void hdel(String key, Object... item) {  
    redisTemplate.opsForHash().delete(key, item);  
}

/\*\*  
 \* 判断hash表中是否有该项的值  
 \*  
 \* @param key  键 不能为null  
 \* @param item 项 不能为null  
 \* @return true 存在 false不存在  
 \*/  
public boolean hHasKey(String key, String item) {  
    return redisTemplate.opsForHash().hasKey(key, item);  
}

/\*\*  
 \* hash递增 如果不存在,就会创建一个 并把新增后的值返回  
 \*  
 \* @param key  键  
 \* @param item 项  
 \* @param by   要增加几(大于0)  
 \* @return  
 \*/  
public double hincr(String key, String item, double by) {  
    return redisTemplate.opsForHash().increment(key, item, by);  
}

/\*\*  
 \* hash递减  
 \*  
 \* @param key  键  
 \* @param item 项  
 \* @param by   要减少记(小于0)  
 \* @return  
 \*/  
public double hdecr(String key, String item, double by) {  
    return redisTemplate.opsForHash().increment(key, item, -by);  
}

//===============================list=================================

/\*\*  
 \* 获取list缓存的内容  
 \*  
 \* @param key   键  
 \* @param start 开始  
 \* @param end   结束  0 到 -1代表所有值  
 \* @return  
 \*/  
public List<Object> lGet(String key, long start, long end) {  
    try {  
        return redisTemplate.opsForList().range(key, start, end);  
    } catch (Exception e) {  
        e.printStackTrace();  
        return null;  
    }  
}

/\*\*  
 \* 获取list缓存的长度  
 \*  
 \* @param key 键  
 \* @return  
 \*/  
public long lGetListSize(String key) {  
    try {  
        return redisTemplate.opsForList().size(key);  
    } catch (Exception e) {  
        e.printStackTrace();  
        return 0;  
    }  
}

/\*\*  
 \* 通过索引 获取list中的值  
 \*  
 \* @param key   键  
 \* @param index 索引  index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推  
 \* @return  
 \*/  
public Object lGetIndex(String key, long index) {  
    try {  
        return redisTemplate.opsForList().index(key, index);  
    } catch (Exception e) {  
        e.printStackTrace();  
        return null;  
    }  
}

/\*\*  
 \* 将list放入缓存  
 \*  
 \* @param key   键  
 \* @param value 值  
 \* @return  
 \*/  
public boolean lSet(String key, Object value) {  
    try {  
        redisTemplate.opsForList().rightPush(key, value);  
        return true;  
    } catch (Exception e) {  
        e.printStackTrace();  
        return false;  
    }  
}

/\*\*  
 \* 将list放入缓存  
 \*  
 \* @param key   键  
 \* @param value 值  
 \* @param time  时间(秒)  
 \* @return  
 \*/  
public boolean lSet(String key, Object value, long time) {  
    try {  
        redisTemplate.opsForList().rightPush(key, value);  
        if (time > 0) {  
            expire(key, time);  
        }  
        return true;  
    } catch (Exception e) {  
        e.printStackTrace();  
        return false;  
    }  
}

/\*\*  
 \* 将list放入缓存  
 \*  
 \* @param key   键  
 \* @param value 值  
 \* @return  
 \*/  
public boolean lSet(String key, List<Object> value) {  
    try {  
        redisTemplate.opsForList().rightPushAll(key, value);  
        return true;  
    } catch (Exception e) {  
        e.printStackTrace();  
        return false;  
    }  
}

/\*\*  
 \* 将list放入缓存  
 \*  
 \* @param key   键  
 \* @param value 值  
 \* @param time  时间(秒)  
 \* @return  
 \*/  
public boolean lSet(String key, List<Object> value, long time) {  
    try {  
        redisTemplate.opsForList().rightPushAll(key, value);  
        if (time > 0) {  
            expire(key, time);  
        }  
        return true;  
    } catch (Exception e) {  
        e.printStackTrace();  
        return false;  
    }  
}

/\*\*  
 \* 根据索引修改list中的某条数据  
 \*  
 \* @param key   键  
 \* @param index 索引  
 \* @param value 值  
 \* @return  
 \*/  
public boolean lUpdateIndex(String key, long index, Object value) {  
    try {  
        redisTemplate.opsForList().set(key, index, value);  
        return true;  
    } catch (Exception e) {  
        e.printStackTrace();  
        return false;  
    }  
}

/\*\*  
 \* 移除N个值为value  
 \*  
 \* @param key   键  
 \* @param count 移除多少个  
 \* @param value 值  
 \* @return 移除的个数  
 \*/  
public long lRemove(String key, long count, Object value) {  
    try {  
        Long remove = redisTemplate.opsForList().remove(key, count, value);  
        return remove;  
    } catch (Exception e) {  
        e.printStackTrace();  
        return 0;  
    }  
}  
//============================set=============================

/\*\*  
 \* 根据key获取Set中的所有值  
 \*  
 \* @param key 键  
 \* @return  
 \*/  
public Set<Object> sGet(String key) {  
    try {  
        return redisTemplate.opsForSet().members(key);  
    } catch (Exception e) {  
        e.printStackTrace();  
        return null;  
    }  
}

/\*\*  
 \* 根据value从一个set中查询,是否存在  
 \*  
 \* @param key   键  
 \* @param value 值  
 \* @return true 存在 false不存在  
 \*/  
public boolean sHasKey(String key, Object value) {  
    try {  
        return redisTemplate.opsForSet().isMember(key, value);  
    } catch (Exception e) {  
        e.printStackTrace();  
        return false;  
    }  
}

/\*\*  
 \* 将数据放入set缓存  
 \*  
 \* @param key    键  
 \* @param values 值 可以是多个  
 \* @return 成功个数  
 \*/  
public long sSet(String key, Object... values) {  
    try {  
        return redisTemplate.opsForSet().add(key, values);  
    } catch (Exception e) {  
        e.printStackTrace();  
        return 0;  
    }  
}

/\*\*  
 \* 将set数据放入缓存  
 \*  
 \* @param key    键  
 \* @param time   时间(秒)  
 \* @param values 值 可以是多个  
 \* @return 成功个数  
 \*/  
public long sSetAndTime(String key, long time, Object... values) {  
    try {  
        Long count = redisTemplate.opsForSet().add(key, values);  
        if (time > 0) {  
            expire(key, time);  
        }  
        return count;  
    } catch (Exception e) {  
        e.printStackTrace();  
        return 0;  
    }  
}

/\*\*  
 \* 获取set缓存的长度  
 \*  
 \* @param key 键  
 \* @return  
 \*/  
public long sGetSetSize(String key) {  
    try {  
        return redisTemplate.opsForSet().size(key);  
    } catch (Exception e) {  
        e.printStackTrace();  
        return 0;  
    }  
}

/\*\*  
 \* 移除值为value的  
 \*  
 \* @param key    键  
 \* @param values 值 可以是多个  
 \* @return 移除的个数  
 \*/  
public long setRemove(String key, Object... values) {  
    try {  
        Long count = redisTemplate.opsForSet().remove(key, values);  
        return count;  
    } catch (Exception e) {  
        e.printStackTrace();  
        return 0;  
    }  
}

//====================================================zSet 有序集合=======================

/\*\*  
 \* 添加元素  ,参数分别是  键 ,分数 ,字段名  
 \*/  

//
public Boolean zadd(String key, double score, String member) {
// System.out.println(key);
// System.out.println(score);
// System.out.println(member);
return redisTemplate.opsForZSet().add(key, member, score);
}

/\*\*  
 \* 获取元素数量  
 \*/  
public long zcard(String key) {  
    try {  
        return redisTemplate.opsForZSet().zCard(key);  
    } catch (Exception e) {  
        e.printStackTrace();  
        return 0;  
    }  
}

/\*\*  
 \* 查询集合中指定顺序的值, 0 -1 表示获取全部的集合内容  zrange  
 \* <p>  
 \* 返回有序的集合,score小的在前面  
 \*/  
public Set<Object> zrange(String key, long start, long end) {  
    return redisTemplate.opsForZSet().range(key, start, end);  
}

/\*\*  
 \* 删除某个或多个字段  
 \*/  
public long zrem(String key, Object... members) {  
    try {  
        return redisTemplate.opsForZSet().remove(key, members);  
    } catch (Exception e) {  
        e.printStackTrace();  
        return 0;  
    }

}

/\*\*  
 \* 查询value对应的score   zscore  
 \* <p>  
 \* 当value在集合中时,返回其score;如果不在,则返回null  
 \*/  
public Double score(String key, String value) {  
    return redisTemplate.opsForZSet().score(key, value);  
}

/\*\*  
 \* 判断value在zset中的排名  zrank  
 \* <p>  
 \* 这里score越小排名越高;  
 \*/  
public long rank(String key, String value) {  
    try {  
        return redisTemplate.opsForZSet().rank(key, value);  
    } catch (  
            Exception e) {  
        e.printStackTrace();  
        return 0;  
    }  
}

/\*\*  
 \* score的增加or减少 zincrby  
 \*/  
public Double incrScore(String key, String value, double score) {  
    return redisTemplate.opsForZSet().incrementScore(key, value, score);  
}

/\*\*  
 \* 返回集合的长度  
 \*/  
public long size(String key) {  
    try {  
        return redisTemplate.opsForZSet().zCard(key);  
    } catch (Exception e) {  
        e.printStackTrace();  
        return 0;  
    }  
}

/\*\*  
 \* 查询集合中指定顺序的值和score,0, -1 表示获取全部的集合内容  
 \*/  
public Set<ZSetOperations.TypedTuple<Object>> rangeWithScore(String key, int start, int end) {  
    return redisTemplate.opsForZSet().rangeWithScores(key, start, end);  
}

/\*\*  
 \* 查询集合中指定顺序的值  zrevrange  
 \* <p>  
 \* 返回有序的集合中,score大的在前面  
 \*/  
public Set<Object> revRange(String key, int start, int end) {  
    return redisTemplate.opsForZSet().reverseRange(key, start, end);  
}

/\*\*  
 \* 根据score的值,来获取满足条件的集合  zrangebyscore  
 \*/  
public Set<Object> sortRange(String key, int min, int max) {  
    return redisTemplate.opsForZSet().rangeByScore(key, min, max);  
}

}

【补充springMVC的封装工具,可参考使用

package cn.cen2guo.clinic.redis;

import java.util.List;
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.SortingParams;

import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.util.SafeEncoder;

/*
下面这两个在高版本的redis.clients依赖包里面没有,需要降低版本才有,如2.9.0
*import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.util.SafeEncoder;
*/

public class JedisUtil {
/**
* 缓存生存时间
*/
private final int expire = 60000;
/**
* 操作Key的方法
*/
public Keys KEYS;
/**
* 对存储结构为Set类型的操作
*/
public Sets SETS;
/**
* 对存储结构为HashMap类型的操作
*/
public Hash HASH;
/**
* 对存储结构为String类型的操作
*/
public Strings STRINGS;
/**
* 对存储结构为List类型的操作
*/
public Lists LISTS;
/*** 有序队列*/
public ZSets ZSets;
/**
* 获取jedis对象,用于锁的使用
*/
public Jedis MyGetJedis;

//  
//  
//

private JedisPool jedisPool;

public JedisPool getJedisPool() {  
    return jedisPool;  
}

public void setJedisPool(JedisPoolWriper jedisPoolWriper) {  
    this.jedisPool = jedisPoolWriper.getJedisPool();  
}

public JedisPool getPool() {  
    return jedisPool;  
}

/\*\*  
 \* 从jedis连接池中获取获取jedis对象  
 \*  
 \* @return  
 \*/  
public Jedis getJedis() {  
    return jedisPool.getResource();  
}

/\*\*  
 \* 设置过期时间  
 \*  
 \* @param key  
 \* @param seconds  
 \* @author ruan 2013-4-11  
 \*/  
public void expire(String key, int seconds) {  
    if (seconds <= 0) {  
        return;  
    }  
    Jedis jedis = getJedis();  
    jedis.expire(key, seconds);  
    jedis.close();  
}

/\*\*  
 \* 设置默认过期时间  
 \*  
 \* @param key  
 \* @author ruan 2013-4-11  
 \*/  
public void expire(String key) {  
    expire(key, expire);  
}

// \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*Keys\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*//  
public class Keys {

    public Keys(JedisUtil jedisUtil) {

    }

    /\*\*  
     \* 清空所有key  
     \*/  
    public String flushAll() {  
        Jedis jedis = getJedis();  
        String stata = jedis.flushAll();  
        jedis.close();  
        return stata;  
    }

    /\*\*  
     \* 更改key  
     \* <p>  
     \* //         \* @param String  
     \* oldkey  
     \* //         \* @param String  
     \* newkey  
     \*  
     \* @return 状态码  
     \*/  
    public String rename(String oldkey, String newkey) {  
        return rename(SafeEncoder.encode(oldkey),  
                SafeEncoder.encode(newkey));  
    }

    /\*\*  
     \* 更改key,仅当新key不存在时才执行  
     \* <p>  
     \* //         \* @param String  
     \* oldkey  
     \* //         \* @param String  
     \* newkey  
     \*  
     \* @return 状态码  
     \*/  
    public long renamenx(String oldkey, String newkey) {  
        Jedis jedis = getJedis();  
        long status = jedis.renamenx(oldkey, newkey);  
        jedis.close();  
        return status;  
    }

    /\*\*  
     \* 更改key  
     \* <p>  
     \* //         \* @param String  
     \* oldkey  
     \* //         \* @param String  
     \* newkey  
     \*  
     \* @return 状态码  
     \*/  
    public String rename(byte\[\] oldkey, byte\[\] newkey) {  
        Jedis jedis = getJedis();  
        String status = jedis.rename(oldkey, newkey);  
        jedis.close();  
        return status;  
    }

    /\*\*  
     \* 设置key的过期时间,以秒为单位  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \* //         \* @param 时间  
     \* ,已秒为单位  
     \*  
     \* @return 影响的记录数  
     \*/  
    public long expired(String key, int seconds) {  
        Jedis jedis = getJedis();  
        long count = jedis.expire(key, seconds);  
        jedis.close();  
        return count;  
    }

    /\*\*  
     \* 设置key的过期时间,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00,格里高利历)的偏移量。  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \* //         \* @param 时间  
     \* ,已秒为单位  
     \*  
     \* @return 影响的记录数  
     \*/  
    public long expireAt(String key, long timestamp) {  
        Jedis jedis = getJedis();  
        long count = jedis.expireAt(key, timestamp);  
        jedis.close();  
        return count;  
    }

    /\*\*  
     \* 查询key的过期时间  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \*  
     \* @return 以秒为单位的时间表示  
     \*/  
    public long ttl(String key) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        long len = sjedis.ttl(key);  
        sjedis.close();  
        return len;  
    }

    /\*\*  
     \* 取消对key过期时间的设置  
     \*  
     \* @param key  
     \* @return 影响的记录数  
     \*/  
    public long persist(String key) {  
        Jedis jedis = getJedis();  
        long count = jedis.persist(key);  
        jedis.close();  
        return count;  
    }

    /\*\*  
     \* 删除keys对应的记录,可以是多个key  
     \* <p>  
     \* //         \* @param String  
     \* ... keys  
     \*  
     \* @return 删除的记录数  
     \*/  
    public long del(String... keys) {  
        Jedis jedis = getJedis();  
        long count = jedis.del(keys);  
        jedis.close();  
        return count;  
    }

// String…是java5新加入的功能,表示的是一个可变长度的参数列表。
// 其语法就是类型后跟…,表示此处接受的参数为0到多个Object类型的对象,或者是一个Object[]。
// 例如我们有一个方法叫做test(String…strings),那么你还可以写方法test(),但你不能写test(String[] strings),
// 这样会出编译错误,系统提示出现重复的方法。
// 在使用的时候,对于test(String…strings),你可以直接用test()去调用,标示没有参数,也可以用去test(“aaa”),
// 也可以用test(new String[]{“aaa”,”bbb”})。
// 另外如果既有test(String…strings)函数,又有test()函数,我们在调用test()时,会优先使用test()函数。
// 只有当没有test()函数式,我们调用test(),程序才会走test(String…strings)。

    /\*\*  
     \* 删除keys对应的记录,可以是多个key  
     \* <p>  
     \* //         \* @param String  
     \* ... keys  
     \*  
     \* @return 删除的记录数  
     \*/  
    public long del(byte\[\]... keys) {  
        Jedis jedis = getJedis();  
        long count = jedis.del(keys);  
        jedis.close();  
        return count;  
    }

    /\*\*  
     \* 判断key是否存在  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \*  
     \* @return boolean  
     \*/  
    public boolean exists(String key) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        boolean exis = sjedis.exists(key);  
        sjedis.close();  
        return exis;  
    }

    /\*\*  
     \* 对List,Set,SortSet进行排序,如果集合数据较大应避免使用这个方法  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \*  
     \* @return List<String> 集合的全部记录  
     \*\*/  
    public List<String> sort(String key) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        List<String> list = sjedis.sort(key);  
        sjedis.close();  
        return list;  
    }

    /\*\*  
     \* 对List,Set,SortSet进行排序或limit  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \* //         \* @param SortingParams  
     \* parame 定义排序类型或limit的起止位置.  
     \*  
     \* @return List<String> 全部或部分记录  
     \*\*/  
    public List<String> sort(String key, SortingParams parame) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        List<String> list = sjedis.sort(key, parame);  
        sjedis.close();  
        return list;  
    }

    /\*\*  
     \* 返回指定key存储的类型  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \*  
     \* @return String string|list|set|zset|hash  
     \*\*/  
    public String type(String key) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        String type = sjedis.type(key);  
        sjedis.close();  
        return type;  
    }

    /\*\*  
     \* 查找所有匹配给定的模式的键  
     \* <p>  
     \* //         \* @param String  
     \* key的表达式,\*表示多个,?表示一个  
     \*/  
    public Set<String> keys(String pattern) {  
        Jedis jedis = getJedis();  
        Set<String> set = jedis.keys(pattern);  
        jedis.close();  
        return set;  
    }  
}

// \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*Sets\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*//  
public class Sets {

    public Sets(JedisUtil jedisUtil) {

    }

    /\*\*  
     \* 向Set添加一条记录,如果member已存在返回0,否则返回1  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \* //         \* @param String  
     \* member  
     \*  
     \* @return 操作码, 0或1  
     \*/  
    public long sadd(String key, String member) {  
        Jedis jedis = getJedis();  
        long s = jedis.sadd(key, member);  
        jedis.close();  
        return s;  
    }

    public long sadd(byte\[\] key, byte\[\] member) {  
        Jedis jedis = getJedis();  
        long s = jedis.sadd(key, member);  
        jedis.close();  
        return s;  
    }

    /\*\*  
     \* 获取给定key中元素个数  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \*  
     \* @return 元素个数  
     \*/  
    public long scard(String key) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        long len = sjedis.scard(key);  
        sjedis.close();  
        return len;  
    }

    /\*\*  
     \* 返回从第一组和所有的给定集合之间的差异的成员  
     \* <p>  
     \* //         \* @param String  
     \* ... keys  
     \*  
     \* @return 差异的成员集合  
     \*/  
    public Set<String> sdiff(String... keys) {  
        Jedis jedis = getJedis();  
        Set<String> set = jedis.sdiff(keys);  
        jedis.close();  
        return set;  
    }

    /\*\*  
     \* 这个命令等于sdiff,但返回的不是结果集,而是将结果集存储在新的集合中,如果目标已存在,则覆盖。  
     \* <p>  
     \* //         \* @param String  
     \* newkey 新结果集的key  
     \* //         \* @param String  
     \* ... keys 比较的集合  
     \*  
     \* @return 新集合中的记录数  
     \*\*/  
    public long sdiffstore(String newkey, String... keys) {  
        Jedis jedis = getJedis();  
        long s = jedis.sdiffstore(newkey, keys);  
        jedis.close();  
        return s;  
    }

    /\*\*  
     \* 返回给定集合交集的成员,如果其中一个集合为不存在或为空,则返回空Set  
     \* <p>  
     \* //         \* @param String  
     \* ... keys  
     \*  
     \* @return 交集成员的集合  
     \*\*/  
    public Set<String> sinter(String... keys) {  
        Jedis jedis = getJedis();  
        Set<String> set = jedis.sinter(keys);  
        jedis.close();  
        return set;  
    }

    /\*\*  
     \* 这个命令等于sinter,但返回的不是结果集,而是将结果集存储在新的集合中,如果目标已存在,则覆盖。  
     \* <p>  
     \* //         \* @param String  
     \* newkey 新结果集的key  
     \* //         \* @param String  
     \* ... keys 比较的集合  
     \*  
     \* @return 新集合中的记录数  
     \*\*/  
    public long sinterstore(String newkey, String... keys) {  
        Jedis jedis = getJedis();  
        long s = jedis.sinterstore(newkey, keys);  
        jedis.close();  
        return s;  
    }

    /\*\*  
     \* 确定一个给定的值是否存在  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \* //         \* @param String  
     \* member 要判断的值  
     \*  
     \* @return 存在返回1,不存在返回0  
     \*\*/  
    public boolean sismember(String key, String member) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        boolean s = sjedis.sismember(key, member);  
        sjedis.close();  
        return s;  
    }

    /\*\*  
     \* 返回集合中的所有成员  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \*  
     \* @return 成员集合  
     \*/  
    public Set<String> smembers(String key) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        Set<String> set = sjedis.smembers(key);  
        sjedis.close();  
        return set;  
    }

    public Set<byte\[\]> smembers(byte\[\] key) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        Set<byte\[\]> set = sjedis.smembers(key);  
        sjedis.close();  
        return set;  
    }

    /\*\*  
     \* 将成员从源集合移出放入目标集合 <br/>  
     \* 如果源集合不存在或不包哈指定成员,不进行任何操作,返回0<br/>  
     \* 否则该成员从源集合上删除,并添加到目标集合,如果目标集合中成员已存在,则只在源集合进行删除  
     \* <p>  
     \* //         \* @param String  
     \* srckey 源集合  
     \* //         \* @param String  
     \* dstkey 目标集合  
     \* //         \* @param String  
     \* member 源集合中的成员  
     \*  
     \* @return 状态码,1成功,0失败  
     \*/  
    public long smove(String srckey, String dstkey, String member) {  
        Jedis jedis = getJedis();  
        long s = jedis.smove(srckey, dstkey, member);  
        jedis.close();  
        return s;  
    }

    /\*\*  
     \* 从集合中删除成员  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \*  
     \* @return 被删除的成员  
     \*/  
    public String spop(String key) {  
        Jedis jedis = getJedis();  
        String s = jedis.spop(key);  
        jedis.close();  
        return s;  
    }

    /\*\*  
     \* 从集合中删除指定成员  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \* //         \* @param String  
     \* member 要删除的成员  
     \*  
     \* @return 状态码,成功返回1,成员不存在返回0  
     \*/  
    public long srem(String key, String member) {  
        Jedis jedis = getJedis();  
        long s = jedis.srem(key, member);  
        jedis.close();  
        return s;  
    }

    /\*\*  
     \* 合并多个集合并返回合并后的结果,合并后的结果集合并不保存<br/>  
     \* <p>  
     \* //         \* @param String  
     \* ... keys  
     \*  
     \* @return 合并后的结果集合  
     \* //         \* @see sunionstore  
     \*/  
    public Set<String> sunion(String... keys) {  
        Jedis jedis = getJedis();  
        Set<String> set = jedis.sunion(keys);  
        jedis.close();  
        return set;  
    }

    /\*\*  
     \* 合并多个集合并将合并后的结果集保存在指定的新集合中,如果新集合已经存在则覆盖  
     \* <p>  
     \* //         \* @param String  
     \* newkey 新集合的key  
     \* //         \* @param String  
     \* ... keys 要合并的集合  
     \*\*/  
    public long sunionstore(String newkey, String... keys) {  
        Jedis jedis = getJedis();  
        long s = jedis.sunionstore(newkey, keys);  
        jedis.close();  
        return s;  
    }  
}

// \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*Hash\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*//  
public class Hash {

    public Hash(JedisUtil jedisUtil) {

    }

    /\*\*  
     \* 从hash中删除指定的存储  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \* //         \* @param String  
     \* fieid 存储的名字  
     \*  
     \* @return 状态码,1成功,0失败  
     \*/  
    public long hdel(String key, String fieid) {  
        Jedis jedis = getJedis();  
        long s = jedis.hdel(key, fieid);  
        jedis.close();  
        return s;  
    }

    public long hdel(String key) {  
        Jedis jedis = getJedis();  
        long s = jedis.del(key);  
        jedis.close();  
        return s;  
    }

    /\*\*  
     \* 测试hash中指定的存储是否存在  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \* //         \* @param String  
     \* fieid 存储的名字  
     \*  
     \* @return 1存在,0不存在  
     \*/  
    public boolean hexists(String key, String fieid) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        boolean s = sjedis.hexists(key, fieid);  
        sjedis.close();  
        return s;  
    }

    /\*\*  
     \* 返回hash中指定存储位置的值  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \* //         \* @param String  
     \* fieid 存储的名字  
     \*  
     \* @return 存储对应的值  
     \*/  
    public String hget(String key, String fieid) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        String s = sjedis.hget(key, fieid);  
        sjedis.close();  
        return s;  
    }

    public byte\[\] hget(byte\[\] key, byte\[\] fieid) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        byte\[\] s = sjedis.hget(key, fieid);  
        sjedis.close();  
        return s;  
    }

    /\*\*  
     \* 以Map的形式返回hash中的存储和值  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \*  
     \* @return Map<Strinig, String>  
     \*/  
    public Map<String, String> hgetAll(String key) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        Map<String, String> map = sjedis.hgetAll(key);  
        sjedis.close();  
        return map;  
    }

    /\*\*  
     \* 添加一个对应关系  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \* //         \* @param String  
     \* fieid  
     \* //         \* @param String  
     \* value  
     \*  
     \* @return 状态码 1成功,0失败,fieid已存在将更新,也返回0  
     \*\*/  
    public long hset(String key, String fieid, String value) {  
        Jedis jedis = getJedis();  
        long s = jedis.hset(key, fieid, value);  
        jedis.close();  
        return s;  
    }

    public long hset(String key, String fieid, byte\[\] value) {  
        Jedis jedis = getJedis();  
        long s = jedis.hset(key.getBytes(), fieid.getBytes(), value);  
        jedis.close();  
        return s;  
    }

    /\*\*  
     \* 添加对应关系,只有在fieid不存在时才执行  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \* //         \* @param String  
     \* fieid  
     \* //         \* @param String  
     \* value  
     \*  
     \* @return 状态码 1成功,0失败fieid已存  
     \*\*/  
    public long hsetnx(String key, String fieid, String value) {  
        Jedis jedis = getJedis();  
        long s = jedis.hsetnx(key, fieid, value);  
        jedis.close();  
        return s;  
    }

    /\*\*  
     \* 获取hash中value的集合  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \*  
     \* @return List<String>  
     \*/  
    public List<String> hvals(String key) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        List<String> list = sjedis.hvals(key);  
        sjedis.close();  
        return list;  
    }

    /\*\*  
     \* 在指定的存储位置加上指定的数字,存储位置的值必须可转为数字类型  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \* //         \* @param String  
     \* fieid 存储位置  
     \* //         \* @param String  
     \* long value 要增加的值,可以是负数  
     \*  
     \* @return 增加指定数字后,存储位置的值  
     \*/  
    public long hincrby(String key, String fieid, long value) {  
        Jedis jedis = getJedis();  
        long s = jedis.hincrBy(key, fieid, value);  
        jedis.close();  
        return s;  
    }

    /\*\*  
     \* 返回指定hash中的所有存储名字,类似Map中的keySet方法  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \*  
     \* @return Set<String> 存储名称的集合  
     \*/  
    public Set<String> hkeys(String key) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        Set<String> set = sjedis.hkeys(key);  
        sjedis.close();  
        return set;  
    }

    /\*\*  
     \* 获取hash中存储的个数,类似Map中size方法  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \*  
     \* @return long 存储的个数  
     \*/  
    public long hlen(String key) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        long len = sjedis.hlen(key);  
        sjedis.close();  
        return len;  
    }

    /\*\*  
     \* 根据多个key,获取对应的value,返回List,如果指定的key不存在,List对应位置为null  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \* //         \* @param String  
     \* ... fieids 存储位置  
     \*  
     \* @return List<String>  
     \*/  
    public List<String> hmget(String key, String... fieids) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        List<String> list = sjedis.hmget(key, fieids);  
        sjedis.close();  
        return list;  
    }

    public List<byte\[\]> hmget(byte\[\] key, byte\[\]... fieids) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        List<byte\[\]> list = sjedis.hmget(key, fieids);  
        sjedis.close();  
        return list;  
    }

    /\*\*  
     \* 添加对应关系,如果对应关系已存在,则覆盖  
     \* <p>  
     \* //         \* @param Strin  
     \* key  
     \* //         \* @param Map  
     \* <String,String> 对应关系  
     \*  
     \* @return 状态,成功返回OK  
     \*/  
    public String hmset(String key, Map<String, String> map) {  
        Jedis jedis = getJedis();  
        String s = jedis.hmset(key, map);  
        jedis.close();  
        return s;  
    }

    /\*\*  
     \* 添加对应关系,如果对应关系已存在,则覆盖  
     \* <p>  
     \* //         \* @param Strin  
     \* key  
     \* //         \* @param Map  
     \* <String,String> 对应关系  
     \*  
     \* @return 状态,成功返回OK  
     \*/  
    public String hmset(byte\[\] key, Map<byte\[\], byte\[\]> map) {  
        Jedis jedis = getJedis();  
        String s = jedis.hmset(key, map);  
        jedis.close();  
        return s;  
    }

}

// \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*Strings\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*//  
public class Strings {

    public Strings(JedisUtil jedisUtil) {

    }

    /\*\*  
     \* 根据key获取记录  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \*  
     \* @return 值  
     \*/  
    public String get(String key) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        String value = sjedis.get(key);  
        sjedis.close();  
        return value;  
    }

    /\*\*  
     \* 根据key获取记录  
     \* <p>  
     \* //         \* @param byte\[\] key  
     \*  
     \* @return 值  
     \*/  
    public byte\[\] get(byte\[\] key) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        byte\[\] value = sjedis.get(key);  
        sjedis.close();  
        return value;  
    }

    /\*\*  
     \* 添加有过期时间的记录  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \* //         \* @param int seconds 过期时间,以秒为单位  
     \* //         \* @param String  
     \* value  
     \*  
     \* @return String 操作状态  
     \*/  
    public String setEx(String key, int seconds, String value) {  
        Jedis jedis = getJedis();  
        String str = jedis.setex(key, seconds, value);  
        jedis.close();  
        return str;  
    }

    /\*\*  
     \* 添加有过期时间的记录  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \* //         \* @param int seconds 过期时间,以秒为单位  
     \* //         \* @param String  
     \* value  
     \*  
     \* @return String 操作状态  
     \*/  
    public String setEx(byte\[\] key, int seconds, byte\[\] value) {  
        Jedis jedis = getJedis();  
        String str = jedis.setex(key, seconds, value);  
        jedis.close();  
        return str;  
    }

    /\*\*  
     \* 添加一条记录,仅当给定的key不存在时才插入  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \* //         \* @param String  
     \* value  
     \*  
     \* @return long 状态码,1插入成功且key不存在,0未插入,key存在  
     \*/  
    public long setnx(String key, String value) {  
        Jedis jedis = getJedis();  
        long str = jedis.setnx(key, value);  
        jedis.close();  
        return str;  
    }

    /\*\*  
     \* 添加记录,如果记录已存在将覆盖原有的value  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \* //         \* @param String  
     \* value  
     \*  
     \* @return 状态码  
     \*/  
    public String set(String key, String value) {  
        return set(SafeEncoder.encode(key), SafeEncoder.encode(value));  
    }

    /\*\*  
     \* 添加记录,如果记录已存在将覆盖原有的value  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \* //         \* @param String  
     \* value  
     \*  
     \* @return 状态码  
     \*/  
    public String set(String key, byte\[\] value) {  
        return set(SafeEncoder.encode(key), value);  
    }

    /\*\*  
     \* 添加记录,如果记录已存在将覆盖原有的value  
     \* <p>  
     \* //         \* @param byte\[\] key  
     \* //         \* @param byte\[\] value  
     \*  
     \* @return 状态码  
     \*/  
    public String set(byte\[\] key, byte\[\] value) {  
        Jedis jedis = getJedis();  
        String status = jedis.set(key, value);  
        jedis.close();  
        return status;  
    }

    /\*\*  
     \* 从指定位置开始插入数据,插入的数据会覆盖指定位置以后的数据<br/>  
     \* 例:String str1="123456789";<br/>  
     \* 对str1操作后setRange(key,4,0000),str1="123400009";  
     \* <p>  
     \* //         \* @param String  
     \* //         \*            key  
     \* //         \* @param long offset  
     \* //         \* @param String  
     \* value  
     \*  
     \* @return long value的长度  
     \*/  
    public long setRange(String key, long offset, String value) {  
        Jedis jedis = getJedis();  
        long len = jedis.setrange(key, offset, value);  
        jedis.close();  
        return len;  
    }

    /\*\*  
     \* 在指定的key中追加value  
     \* <p>  
     \* //         \* @param String  
     \* //         \*            key  
     \* //         \* @param String  
     \* value  
     \*  
     \* @return long 追加后value的长度  
     \*\*/  
    public long append(String key, String value) {  
        Jedis jedis = getJedis();  
        long len = jedis.append(key, value);  
        jedis.close();  
        return len;  
    }

    /\*\*  
     \* 将key对应的value减去指定的值,只有value可以转为数字时该方法才可用  
     \* <p>  
     \* //         \* @param String  
     \* //         \*            key  
     \* //         \* @param long number 要减去的值  
     \*  
     \* @return long 减指定值后的值  
     \*/  
    public long decrBy(String key, long number) {  
        Jedis jedis = getJedis();  
        long len = jedis.decrBy(key, number);  
        jedis.close();  
        return len;  
    }

    /\*\*  
     \* <b>可以作为获取唯一id的方法</b><br/>  
     \* 将key对应的value加上指定的值,只有value可以转为数字时该方法才可用  
     \* <p>  
     \* //         \* @param String  
     \* //         \*            key  
     \* //         \* @param long number 要减去的值  
     \*  
     \* @return long 相加后的值  
     \*/  
    public long incrBy(String key, long number) {  
        Jedis jedis = getJedis();  
        long len = jedis.incrBy(key, number);  
        jedis.close();  
        return len;  
    }

    /\*\*  
     \* 对指定key对应的value进行截取  
     \* <p>  
     \* //         \* @param String  
     \* //         \*            key  
     \* //         \* @param long startOffset 开始位置(包含)  
     \* //         \* @param long endOffset 结束位置(包含)  
     \*  
     \* @return String 截取的值  
     \*/  
    public String getrange(String key, long startOffset, long endOffset) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        String value = sjedis.getrange(key, startOffset, endOffset);  
        sjedis.close();  
        return value;  
    }

    /\*\*  
     \* 获取并设置指定key对应的value<br/>  
     \* 如果key存在返回之前的value,否则返回null  
     \* <p>  
     \* //         \* @param String  
     \* //         \*            key  
     \* //         \* @param String  
     \* value  
     \*  
     \* @return String 原始value或null  
     \*/  
    public String getSet(String key, String value) {  
        Jedis jedis = getJedis();  
        String str = jedis.getSet(key, value);  
        jedis.close();  
        return str;  
    }

    /\*\*  
     \* 批量获取记录,如果指定的key不存在返回List的对应位置将是null  
     \* <p>  
     \* //         \* @param String  
     \* keys  
     \*  
     \* @return List<String> 值得集合  
     \*/  
    public List<String> mget(String... keys) {  
        Jedis jedis = getJedis();  
        List<String> str = jedis.mget(keys);  
        jedis.close();  
        return str;  
    }

    /\*\*  
     \* 批量存储记录  
     \* <p>  
     \* //         \* @param String  
     \* keysvalues 例:keysvalues="key1","value1","key2","value2";  
     \*  
     \* @return String 状态码  
     \*/  
    public String mset(String... keysvalues) {  
        Jedis jedis = getJedis();  
        String str = jedis.mset(keysvalues);  
        jedis.close();  
        return str;  
    }

    /\*\*  
     \* 获取key对应的值的长度  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \*  
     \* @return value值得长度  
     \*/  
    public long strlen(String key) {  
        Jedis jedis = getJedis();  
        long len = jedis.strlen(key);  
        jedis.close();  
        return len;  
    }  
}

// \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*Lists\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*//  
public class Lists {

    public Lists(JedisUtil jedisUtil) {

    }

    /\*\*  
     \* List长度  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \*  
     \* @return 长度  
     \*/  
    public long llen(String key) {  
        return llen(SafeEncoder.encode(key));  
    }

    /\*\*  
     \* List长度  
     \* <p>  
     \* //         \* @param byte\[\] key  
     \*  
     \* @return 长度  
     \*/  
    public long llen(byte\[\] key) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        long count = sjedis.llen(key);  
        sjedis.close();  
        return count;  
    }

    /\*\*  
     \* 覆盖操作,将覆盖List中指定位置的值  
     \* <p>  
     \* //         \* @param byte\[\] key  
     \* //         \* @param int index 位置  
     \* //         \* @param byte\[\] value 值  
     \*  
     \* @return 状态码  
     \*/  
    public String lset(byte\[\] key, int index, byte\[\] value) {  
        Jedis jedis = getJedis();  
        String status = jedis.lset(key, index, value);  
        jedis.close();  
        return status;  
    }

    /\*\*  
     \* 覆盖操作,将覆盖List中指定位置的值  
     \* <p>  
     \* //         \* @param key  
     \* //         \* @param int index 位置  
     \* //         \* @param String  
     \* value 值  
     \*  
     \* @return 状态码  
     \*/  
    public String lset(String key, int index, String value) {  
        return lset(SafeEncoder.encode(key), index,  
                SafeEncoder.encode(value));  
    }

    /\*\*  
     \* 在value的相对位置插入记录  
     \* <p>  
     \* //         \* @param key  
     \* //         \* @param LIST\_POSITION  
     \* //         \*            前面插入或后面插入  
     \* //         \* @param String  
     \* //         \*            pivot 相对位置的内容  
     \* //         \* @param String  
     \* //         \*            value 插入的内容  
     \*  
     \* @return 记录总数  
     \*/  
    public long linsert(String key, LIST\_POSITION where, String pivot,  
                        String value) {  
        return linsert(SafeEncoder.encode(key), where,  
                SafeEncoder.encode(pivot), SafeEncoder.encode(value));  
    }

    /\*\*  
     \* 在指定位置插入记录  
     \* <p>  
     \* //         \* @param String  
     \* //         \*            key  
     \* //         \* @param LIST\_POSITION  
     \* //         \*            前面插入或后面插入  
     \* //         \* @param byte\[\] pivot 相对位置的内容  
     \* //         \* @param byte\[\] value 插入的内容  
     \*  
     \* @return 记录总数  
     \*/  
    public long linsert(byte\[\] key, LIST\_POSITION where, byte\[\] pivot,  
                        byte\[\] value) {  
        Jedis jedis = getJedis();  
        long count = jedis.linsert(key, where, pivot, value);  
        jedis.close();  
        return count;  
    }

    /\*\*  
     \* 获取List中指定位置的值  
     \* <p>  
     \* //         \* @param String  
     \* //         \*            key  
     \* //         \* @param int index 位置  
     \*  
     \* @return 值  
     \*\*/  
    public String lindex(String key, int index) {  
        return SafeEncoder.encode(lindex(SafeEncoder.encode(key), index));  
    }

    /\*\*  
     \* 获取List中指定位置的值  
     \* <p>  
     \* //         \* @param byte\[\] key  
     \* //         \* @param int index 位置  
     \* //         \* @return 值  
     \*\*/  
    public byte\[\] lindex(byte\[\] key, int index) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        byte\[\] value = sjedis.lindex(key, index);  
        sjedis.close();  
        return value;  
    }

    /\*\*  
     \* 将List中的第一条记录移出List  
     \* <p>  
     \* //         \* @param String  
     \* key  
     \*  
     \* @return 移出的记录  
     \*/  
    public String lpop(String key) {  
        return SafeEncoder.encode(lpop(SafeEncoder.encode(key)));  
    }

    /\*\*  
     \* 将List中的第一条记录移出List  
     \* <p>  
     \* //         \* @param byte\[\] key  
     \*  
     \* @return 移出的记录  
     \*/  
    public byte\[\] lpop(byte\[\] key) {  
        Jedis jedis = getJedis();  
        byte\[\] value = jedis.lpop(key);  
        jedis.close();  
        return value;  
    }

    /\*\*  
     \* 将List中最后第一条记录移出List  
     \* <p>  
     \* //         \* @param byte\[\] key  
     \*  
     \* @return 移出的记录  
     \*/  
    public String rpop(String key) {  
        Jedis jedis = getJedis();  
        String value = jedis.rpop(key);  
        jedis.close();  
        return value;  
    }

    /\*\*  
     \* 向List尾部追加记录  
     \* <p>  
     \* //         \* @param String  
     \* //         \*            key  
     \* //         \* @param String  
     \* value  
     \*  
     \* @return 记录总数  
     \*/  
    public long lpush(String key, String value) {  
        return lpush(SafeEncoder.encode(key), SafeEncoder.encode(value));  
    }

    /\*\*  
     \* 向List头部追加记录  
     \* <p>  
     \* //         \* @param String  
     \* //         \*            key  
     \* //         \* @param String  
     \* value  
     \*  
     \* @return 记录总数  
     \*/  
    public long rpush(String key, String value) {  
        Jedis jedis = getJedis();  
        long count = jedis.rpush(key, value);  
        jedis.close();  
        return count;  
    }

    /\*\*  
     \* 向List头部追加记录  
     \* <p>  
     \* //         \* @param String  
     \* //         \*            key  
     \* //         \* @param String  
     \* value  
     \*  
     \* @return 记录总数  
     \*/  
    public long rpush(byte\[\] key, byte\[\] value) {  
        Jedis jedis = getJedis();  
        long count = jedis.rpush(key, value);  
        jedis.close();  
        return count;  
    }

    /\*\*  
     \* 向List中追加记录  
     \* <p>  
     \* //         \* @param byte\[\] key  
     \* //         \* @param byte\[\] value  
     \*  
     \* @return 记录总数  
     \*/  
    public long lpush(byte\[\] key, byte\[\] value) {  
        Jedis jedis = getJedis();  
        long count = jedis.lpush(key, value);  
        jedis.close();  
        return count;  
    }

    /\*\*  
     \* 获取指定范围的记录,可以做为分页使用  
     \* <p>  
     \* //         \* @param String  
     \* //         \*            key  
     \* //         \* @param long start  
     \* //         \* @param long end  
     \*  
     \* @return List  
     \*/  
    public List<String> lrange(String key, long start, long end) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        List<String> list = sjedis.lrange(key, start, end);  
        sjedis.close();  
        return list;  
    }

    /\*\*  
     \* 获取指定范围的记录,可以做为分页使用  
     \* <p>  
     \* //         \* @param byte\[\] key  
     \* //         \* @param int start  
     \* //         \* @param int end 如果为负数,则尾部开始计算  
     \*  
     \* @return List  
     \*/  
    public List<byte\[\]> lrange(byte\[\] key, int start, int end) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        List<byte\[\]> list = sjedis.lrange(key, start, end);  
        sjedis.close();  
        return list;  
    }

    /\*\*  
     \* 删除List中c条记录,被删除的记录值为value  
     \* <p>  
     \* //         \* @param byte\[\] key  
     \* //         \* @param int c 要删除的数量,如果为负数则从List的尾部检查并删除符合的记录  
     \* //         \* @param byte\[\] value 要匹配的值  
     \*  
     \* @return 删除后的List中的记录数  
     \*/  
    public long lrem(byte\[\] key, int c, byte\[\] value) {  
        Jedis jedis = getJedis();  
        long count = jedis.lrem(key, c, value);  
        jedis.close();  
        return count;  
    }

    /\*\*  
     \* 删除List中c条记录,被删除的记录值为value  
     \* <p>  
     \* //         \* @param String  
     \* //         \*            key  
     \* //         \* @param int c 要删除的数量,如果为负数则从List的尾部检查并删除符合的记录  
     \* //         \* @param String  
     \* value 要匹配的值  
     \*  
     \* @return 删除后的List中的记录数  
     \*/  
    public long lrem(String key, int c, String value) {  
        return lrem(SafeEncoder.encode(key), c, SafeEncoder.encode(value));  
    }

    /\*\*  
     \* 算是删除吧,只保留start与end之间的记录  
     \* <p>  
     \* //         \* @param byte\[\] key  
     \* //         \* @param int start 记录的开始位置(0表示第一条记录)  
     \* //         \* @param int end 记录的结束位置(如果为-1则表示最后一个,-2,-3以此类推)  
     \*  
     \* @return 执行状态码  
     \*/  
    public String ltrim(byte\[\] key, int start, int end) {  
        Jedis jedis = getJedis();  
        String str = jedis.ltrim(key, start, end);  
        jedis.close();  
        return str;  
    }

    /\*\*  
     \* 算是删除吧,只保留start与end之间的记录  
     \* <p>  
     \* //         \* @param String  
     \* //         \*            key  
     \* //         \* @param int start 记录的开始位置(0表示第一条记录)  
     \* //         \* @param int end 记录的结束位置(如果为-1则表示最后一个,-2,-3以此类推)  
     \*  
     \* @return 执行状态码  
     \*/  
    public String ltrim(String key, int start, int end) {  
        return ltrim(SafeEncoder.encode(key), start, end);  
    }  
}

//====================================================================================================

/\*\*  
 \* 有序集合  
 \*/  
public class ZSets {

    public ZSets(JedisUtil jedisUtil) {

    }

    /\*\*  
     \* 添加元素  
     \*/  
    public long zadd(String key, double score, String member) {  
        System.out.println(key);  
        System.out.println(score);  
        System.out.println(member);  
        Jedis jedis = getJedis();  
        long s = jedis.zadd(key,score, member);  
        jedis.close();  
        return s;  
    }  
    /\*\*  
     \* 获取元素数量  
     \*/  
    public long zcard(String key) {  
        // ShardedJedis sjedis = getShardedJedis();  
        Jedis sjedis = getJedis();  
        long len = sjedis.zcard(key);  
        sjedis.close();  
        return len;  
    }

    /\*\*  
     \* 根据数值获取前 几个 的字段  
     \*/  
    public Set<String> zrange(String key ,long start , long end ) {  
        Jedis jedis = getJedis();  
        Set<String> set = jedis.zrange(key,start,end);  
        jedis.close();  
        return set;  
    }  
    /\*\*  
     \* 删除某个或多个字段  
     \*/  
    public long zrem(String key, String... members) {  
        Jedis jedis = getJedis();  
        long s = jedis.zrem(key, members);  
        jedis.close();  
        return s;  
    }

}

//====================================================================================================  
/\*\*  
 \* 获取jedis对象,用于锁的使用  
 \*/  
public class MyGetJedis{  
    public MyGetJedis(JedisUtil jedisUtil) {

    }

    /\*\*  
     \* 获取jedis对象,传出去  
     \*/  
    public Jedis myGetJedis(){  
        return getJedis();  
    }  
}

}

(6)service层创建接口

package com.example.providerredis8003.service;

import java.util.Map;

public interface EatService {
public Map addFood(String name);

public  Map<String,Object> getAll();  

}

(7)service层接口具体实现类

package com.example.providerredis8003.service.serviceImpl;

import com.example.providerredis8003.service.EatService;
import com.example.providerredis8003.util.RedisUtil;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class EatServiceImpl implements EatService {

@Resource  
private RedisUtil redisUtil;

@Override  
public Map<String, Object> addFood(String name) {  
    Map<String, Object> map = new HashMap<>();  
    if (StringUtils.isBlank(name)) {  
        map.put("data", "name不可为空");  
        return map;  
    }  
    name = "food\_" + name;

    int k = 1;  
    if (redisUtil.hasKey(name)) {  
        k = (int) redisUtil.get(name) + 1;  
    }  
    if (redisUtil.set(name, k)) {  
        map.put("data", "添加成功");  
    } else {  
        map.put("data", "添加失败");  
    }  
    return map;  
}

@Override  
public Map<String, Object> getAll() {  
    Map<String, Object> map = new HashMap<>();  
    Set<String> strings = redisUtil.keys("food\_\*");  
    List<Object> list = new ArrayList<>();  
    for (String s : strings) {  
        list.add(s + "-" + redisUtil.get(s));  
    }  
    map.put("state", 111);  
    map.put("data", list);  
    return map;  
}  

}

注意了 ,调用redis不再是使用 jedis 了 ,在spring boot 是使用 redisTemplate 的 ,在工具的 RedisUtil底层是 使用redisTemplate调用redis指令  ,但 部分语法与jedis有点区别 ,但是不影响使用

(8)controller层

package com.example.providerredis8003.controller;

import com.example.providerredis8003.service.EatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class FoodController {

@Autowired  
private EatService eatService;

/\*\*  
 \* 添加食物,如果已经在redis存在,则数量加一,否则新建  
 \*/  
@RequestMapping("/add")  
public Map<String,Object> addFood(String name){  
    return eatService.addFood(name);  
}

/\*\*  
 \* 获取所有食物  
 \*/  
@RequestMapping("/get")  
public Map<String,Object> getAll(){  
    return eatService.getAll();  
}

@RequestMapping(value = "/getname",method = RequestMethod.GET)  
public String getname(String name){  
    System.out.println("接收名字="+name);  
    return "我是端口8003,你三爷叫:"+name;  
}

}

(9)启动类 【不需要什么配置】

package com.example.providerredis8003;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProviderRedis8003Application {

public static void main(String\[\] args) {  
    SpringApplication.run(ProviderRedis8003Application.class, args);  
}

}

4.测试

(1)访问端口 8003  ,http://localhost:8003/get  ,查询数据

redis无数据 ,但是说明已经连接成功了

(2)访问端口 8003  ,http://localhost:8003/add?name=apple  ,添加数据

添加成功

访问端口 8003  ,http://localhost:8003/add?name=egg  ,添加数据

添加成功

访问端口 8003  ,http://localhost:8003/get  ,查询数据

查询成功,有数据

(3)现在再加一个apple ,

再次 访问端口 8003  ,http://localhost:8003/add?name=apple  ,添加数据

添加成功

再次 访问端口 8003  ,http://localhost:8003/get  ,查询数据

可见再次添加后 apple的数字加 一 了

      成功,撒花!!!

简单的添加、修改 、查询 redis 操作成功完成 ,spring boot成功使用redis ,其他复杂的业务只需要修改redis工具即可完成对redis的操作

-------------------------------------------

参考博文原址:

https://www.jianshu.com/p/b9154316227e

https://blog.csdn.net/User_xiangpeng/article/details/53839702?utm_source=blogxgwz6

https://blog.csdn.net/qianfeng_dashuju/article/details/103859413?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase