spring 集成 redis简单案例

标签: spring  redis  jedis

                                       spring 集成 redis简单案例

1、服务器安装redis,相关步骤见:https://blog.csdn.net/w20228396/article/details/81186344

2、pom.xml引入jar包  

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
			<version>${jedis-version}</version>
        </dependency>


<jedis-version>2.9.0</jedis-version>

3、web.xml会引入spring.xml。在spring中配置引入spring-redis.xml

4、spring-redis.xml配置

<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"

       xsi:schemaLocation="
       http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">


<!-- Redis  无密码配置 -->
    <!--<bean id="jedis" class="redis.clients.jedis.Jedis">
        <constructor-arg index="0" value="47.92.11.23" type="java.lang.String"/>
        <constructor-arg index="1" value="6379"/>
    </bean>-->
    <!-- 加载配置属性文件 -->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:config/redis.properties" />

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="8" /><!-- 最大分配的对象数 -->
        <property name="maxIdle" value="8" /><!-- 最大能够保持idel状态的对象数  -->
        <!-- <property name="minIdle" value="${redis.pool.minIdle}" /> -->
        <property name="maxWaitMillis" value="6000" />
        <property name="testOnBorrow" value="true" /><!-- 当调用borrow Object方法时,是否进行有效性检查 -->
        <property name="testOnReturn" value="true" />
    </bean>
    <!--<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    </bean>-->
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
        <constructor-arg index="0" ref="jedisPoolConfig" />
        <constructor-arg index="1" value="47.92.11.23" />
        <constructor-arg index="2" value="6379" type="int" />
        <constructor-arg index="3" value="2000" type="int" />
        <constructor-arg index="4" value="robot2018"/>
    </bean>

    <bean id="jedisUtil" class="com.yz.dapeng.utils.JedisUtil">
        <property name="jedisPool" ref="jedisPool"/>
    </bean>

</beans>

5、写redis.properties文件

################    development environment     ####################
redis.host=47.92.11.23
redis.port=6379
redis.password=dapeng2018
redis.timeout=2000
redis.session.timeout=1800
redis.pool.maxTotal=8
redis.pool.maxIdle=8
redis.pool.maxWaitMillis=60000
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true

 6、集成到JedisUtil中,写了部分方法,需要其他方法,自己可以再写

package com.yz.dapeng.utils;

import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisException;

import java.util.Map;

public class JedisUtil {

    private static final Logger logger = Logger.getLogger(JedisUtil.class);

    private static JedisPool jedisPool = null;

    public JedisPool getJedisPool() {
        return jedisPool;
    }

    public void setJedisPool(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

    /**
     * 获取资源
     * @return
     * @throws JedisException
     */
    public static Jedis getResource() throws JedisException{
        Jedis jedis = null;
        try {
            jedis =  jedisPool.getResource();
        } catch (JedisException e) {
            logger.warn("getResource.", e);
            returnBrokenResource(jedis);
            throw e;
        }
        return jedis;
    }

    /**
     * 归还资源
     * @param jedis
     */
    public static void returnBrokenResource(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }

    /**
     * 释放资源
     * @param jedis
     */
    public static void returnResource(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }

    /**
     * 往map中添加或更新元素
     * @param key
     * @param field
     * @param value
     */
    public static void hset(String key, String field, String value) {
        Jedis jedis = null;
        try {
            jedis = getResource();
            jedis.hset(key,field,value);
        } catch (JedisException e) {
            e.printStackTrace();
        }finally {
            returnResource(jedis);
        }
    }

    /**
     * 根据key获取map
     * @param key
     * @return
     */
    public static Map<String, String> hgetAll(String key){
        Map<String, String> value = null ;
        Jedis jedis = null;
        try {
            jedis = getResource();
            if (jedis.exists(key)){
                value = jedis.hgetAll(key);
            }
        } catch (JedisException e) {
            e.printStackTrace();
        }finally {
            returnResource(jedis);
        }
        return value;
    }

    /**
     * 从map中删除
     * @param key
     * @param fields
     */
    public static Long hdel(String key, String... fields) {
        Long result = 0L;
        Jedis jedis = null;
        try {
            jedis = getResource();
            if (jedis.exists(key)){
                result = jedis.hdel(key,fields);
            }else{
                logger.debug("key not exists:"+ key);
            }
        } catch (Exception e) {
            logger.warn("hdel error",e);
        } finally {
            returnResource(jedis);
        }
        return result;
    }

}

 完毕。。。

原文链接:加载失败,请重新获取