28redis

我们在项目中访问Redis,不能用控制台命令,而是用客户端代码来访问Redis服务。Redis针对不同的语言有各种各样不同的客户端:

Redis客户端

 

支持Java语言的一些客户端:

 

在这些客户端中,比较优秀的, 企业中使用较多的就是Jedis,因此我们学习Jedis客户端:

=============================

1jdeis 依赖

2 xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<!--设置最大连接数 -->
		<property name="maxTotal" value="${jedis.maxTotal}"/>
	</bean>
	
	<bean class="redis.clients.jedis.ShardedJedisPool">
		<constructor-arg index="0" ref="jedisPoolConfig"/>
		<constructor-arg index="1">
			<list>
				<bean class="redis.clients.jedis.JedisShardInfo">
					<constructor-arg index="0" value="${jedis.node1.ip}"/>
					<constructor-arg index="1" value="${jedis.node1.port}"/>
				</bean>
			</list>
		</constructor-arg>
	</bean>
	

</beans>

properties

jedis.maxTotal=50
jedis.node1.ip=127.0.0.1
jedis.node1.port=6379

3redis工具类

/**
 * 通用的Redis工具类
 * 
 * @author 虎哥
 */
@Service
public class RedisService {
    // 注入分片的连接池
    @Autowired
    private ShardedJedisPool pool;

    /**
     * 执行set命令,添加字符串格式数据
     * 
     * @param key
     * @param value
     * @return
     */
    public String set(final String key, final String value) {
        return execute(new Function<String>() {
            @Override
            public String commond(ShardedJedis jedis) {
                return jedis.set(key, value);
            }
        });
    }

    /**
     * 执行get命令,查询字符串格式数据
     * 
     * @param key
     * @return
     */
    public String get(final String key) {
        return execute(new Function<String>() {
            @Override
            public String commond(ShardedJedis jedis) {
                return jedis.get(key);
            }
        });
    }
    
    /**
     * 执行expire命令,设置存活时间
     * @param key
     * @param seconds
     * @return
     */
    public Long expire(final String key, final Integer seconds){
        return execute(new Function<Long>() {
            @Override
            public Long commond(ShardedJedis jedis) {
                return jedis.expire(key, seconds);
            }
        });
    }
    
    /**
     * 执行set命令,添加字符串格式数据,并且设置存活时间,单位是秒
     * @param key
     * @param value
     * @return
     */
    public String set(final String key, final String value, final Integer seconds) {
        return execute(new Function<String>() {
            @Override
            public String commond(ShardedJedis jedis) {
                String str = jedis.set(key, value);
                jedis.expire(key, seconds);
                return str;
            }
        });
    }
    /**
     * 执行del命令,删除数据
     * @param key
     * @return
     */
    public Long del(final String key){
        return execute(new Function<Long>() {
            @Override
            public Long commond(ShardedJedis jedis) {
                return jedis.del(key);
            }
        });
    }
    // 抽取公用部分代码
    public <R> R execute(Function<R> func) {
        ShardedJedis shardedJedis = null;
        try {
            // 从池中获取连接
            shardedJedis = pool.getResource();
            // 添加数据
            return func.commond(shardedJedis);
        } finally {
            if (null != shardedJedis) {
                shardedJedis.close();
            }
        }
    }

    // 用接口封装要执行的语句,把接口的实现类对象作为参数传递,等同于传递一段代码
    private interface Function<R> {
        // 包含要执行语句的方法,因为返回值未知,所以用泛型
        R commond(ShardedJedis jedis);
    }
}

单元测试:

猜你喜欢

转载自blog.csdn.net/xiaoxiaoniaoQ/article/details/83579270