zoukankan      html  css  js  c++  java
  • spring 集成redis客户端jedis(java)

    spring集成jedis简单实例

     

    jedis是redis的java客户端,spring将redis连接池作为一个bean配置。

    “redis.clients.jedis.JedisPool”,这是单机环境适用的redis连接池。

    1、maven导入相关包:

        <!-- redis依赖包 -->
        <dependency>
          <groupId>redis.clients</groupId>
          <artifactId>jedis</artifactId>
          <version>2.9.0</version>
        </dependency>

    2、下面是单机环境下redis连接池的配置:

       
       

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 引入jedis的properties配置文件 -->
    <!--如果你有多个数据源需要通过<context:property-placeholder管理,且不愿意放在一个配置文件里,那么一定要加上ignore-unresolvable=“true"-->
    <context:property-placeholder location="classpath:properties/redis.properties" ignore-unresolvable="true" />

    <!--Jedis连接池的相关配置-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <!--新版是maxTotal,旧版是maxActive-->
    <property name="maxTotal">
    <value>${redis.pool.maxActive}</value>
    </property>
    <property name="maxIdle">
    <value>${redis.pool.maxIdle}</value>
    </property>
    <property name="testOnBorrow" value="true"/>
    <property name="testOnReturn" value="true"/>
    </bean>

    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
    <constructor-arg name="poolConfig" ref="jedisPoolConfig" />
    <constructor-arg name="host" value="${redis.host}" />
    <constructor-arg name="port" value="${redis.port}" type="int" />
    <constructor-arg name="timeout" value="${redis.timeout}" type="int" />
    <constructor-arg name="password" value="${redis.password}" />
    <constructor-arg name="database" value="${redis.database}" type="int" />
    </bean>
    </beans>

    3、对应的classpath:properties/redis.properties.xml为:

     
    #最大分配的对象数
    redis.pool.maxActive=200
    #最大能够保持idel状态的对象数
    redis.pool.maxIdle=50
    redis.pool.minIdle=10
    redis.pool.maxWaitMillis=20000
    #当池内没有返回对象时,最大等待时间
    redis.pool.maxWait=300
    
    #格式:redis://:[密码]@[服务器地址]:[端口]/[db index]
    redis.uri = redis://:12345@127.0.0.1:6379/0
    
    redis.host = 127.0.0.1
    redis.port = 6379
    redis.timeout=30000
    redis.password = 12345
    redis.database = 0
    复制代码

    4、RedisUtil

    package com.ctid.business.redis.util;

    import javax.annotation.Resource;

    import org.springframework.stereotype.Component;

    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    @Component
    public class RedisUtil {

    @Resource
    private JedisPool jedisPool;// 注入JedisPool

    public String get(String key) {// 获取指定 key 的值。如果 key 不存在,返回 nil
    Jedis jedis = jedisPool.getResource();
    String string = jedis.get(key);
    jedis.close();
    return string;
    }

    public String set(String key, String value) {// 设置一些字符串值
    Jedis jedis = jedisPool.getResource();
    String string = jedis.set(key, value);
    jedis.close();
    return string;
    }

    public String hget(String hkey, String key) {// 获取哈希表中指定字段的值
    Jedis jedis = jedisPool.getResource();
    String string = jedis.hget(hkey, key);
    jedis.close();
    return string;
    }

    public long hset(String hkey, String key, String value) {// 为哈希表中的字段赋值
    Jedis jedis = jedisPool.getResource();
    long result = jedis.hset(hkey, key, value);
    jedis.close();
    return result;
    }

    public long incr(String key) {// 将 key 中储存的数字值增一,如果 key 不存在,那么 key 的值会先被初始化为
    // 0 ,然后再执行INCR操作
    Jedis jedis = jedisPool.getResource();
    long result = jedis.incr(key);
    jedis.close();
    return result;
    }

    public long expire(String key, int second) {// 设置key的到期时间
    Jedis jedis = jedisPool.getResource();
    long result = jedis.expire(key, second);
    jedis.close();
    return result;
    }

    public long ttl(String key) {// 以秒为单位返回 key 的剩余过期时间
    Jedis jedis = jedisPool.getResource();
    long result = jedis.ttl(key);
    jedis.close();
    return result;
    }

    public long del(String key) {// 根据key删除
    Jedis jedis = jedisPool.getResource();
    long result = jedis.del(key);
    jedis.close();
    return result;
    }

    public long hdel(String hkey, String key) {// 删除哈希表key中的一个或多个指定字段
    Jedis jedis = jedisPool.getResource();
    long result = jedis.hdel(hkey, key);
    jedis.close();
    return result;
    }

    }

    5、redisUtil调用

    package com.ctid.business.redis.testIpv6;

    import javax.annotation.Resource;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.stereotype.Service;

    import com.ctid.business.redis.util.RedisUtil;

    @Service
    public class TestIpv6 {

    private static final Log LOGGER = LogFactory.getLog(TestIpv6.class);
    @Resource
    private RedisUtil redisUtil;

    public void testIpv6() {
    LOGGER.info("redis======================================");
    String result1 = redisUtil.set("test", "1111");
    LOGGER.info("redis设置值:" + result1);
    String result2 = redisUtil.get("test");
    LOGGER.info("redis查询值:" + result2);

    }

    }

     
  • 相关阅读:
    oracle 数据库服务名怎么查
    vmware vsphere 6.5
    vSphere虚拟化之ESXi的安装及部署
    ArcMap中无法添加ArcGIS Online底图的诊断方法
    ArcGIS中字段计算器(高级计算VBScript、Python)
    Bad habits : Putting NOLOCK everywhere
    Understanding the Impact of NOLOCK and WITH NOLOCK Table Hints in SQL Server
    with(nolock) or (nolock)
    What is “with (nolock)” in SQL Server?
    Changing SQL Server Collation After Installation
  • 原文地址:https://www.cnblogs.com/pinghengxing/p/9401487.html
Copyright © 2011-2022 走看看