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;

    }
}

 

转载于:https://www.cnblogs.com/langjunnan/p/7703951.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值