redis如何在spring里面的bean配置


 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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.xsd">
    <!--redis連接池配置 -->
    <!--redis 配置 開始 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大活動數目 -->
        <property name="maxActive" value="300" />
        <!-- 最大空數 -->
        <property name="maxIdle" value="100" />
        <!-- 最大等待時間 -->
        <property name="maxWait" value="1000" />
        <!-- true -->
        <property name="testOnBorrow" value="true" />
    </bean>
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool"
        destroy-method="destroy">
        <constructor-arg ref="jedisPoolConfig" />
        <constructor-arg value="127.0.0.1" />
        <constructor-arg value="6379" />
        <constructor-arg value="3000" />
        <!-- pass -->
        <constructor-arg value="1234" />
        <constructor-arg value="0" />
    </bean>
    <!-- RedisApi -->
    <bean id="redisApi" class="cn.geekss.redis.RedisApi">
        <property name="jedisPool" ref="jedisPool"></property>
    </bean>
    <!-- 配置tokenservice -->
    <bean id="tokenService" class="cn.geekss.auth.TokenServiceImpl">
        <property name="redisApi" ref="redisApi"></property>
    </bean>
</beans>

RedisApi類文件

 

 package cn.geekss.redis;

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

/**
 *
 * @author ljn
 * @date 2017-10-20
 * @vesion 1.0
 * @detail api 類
 */
public class RedisApi {

    protected JedisPool jedisPool;

    public JedisPool getJedisPool() {
        return jedisPool;
    }

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

    /**
     * 賦值
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.set(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 設置有效期時間
     *
     * @param key
     * @param seconds
     * @param value
     * @return
     */
    public boolean set(String key, int seconds, String value) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.setex(key, seconds, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 判斷指定key是否存在
     *
     * @return
     */
    public boolean exists(String key) {
        try {
            Jedis jedis = jedisPool.getResource();
            return jedis.exists(key);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return false;

    }

    /**
     * 獲取數據
     *
     * @param key
     * @return
     */
    public String get(String key) {
        Jedis jedis = jedisPool.getResource();

        try {
            return jedis.get(key);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return null;

    }

    /*
     * 刪除指定key
     */
    public boolean del(String key) {
        try {
            Jedis jedis = jedisPool.getResource();

            jedis.del(key);
            return true;
        } catch (Exception e) {
            // TODO: handle exception
        }
        return false;

    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM