redis如何在spring里面的bean配置redis如何在spring里面的bean配置

 

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 ></property>
    </bean>
</beans>

RedisApi类文件

redis如何在spring里面的bean配置

 

 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;

    }
}

 

相关文章:

  • 2022-12-23
  • 2021-07-21
  • 2021-05-28
  • 2021-10-26
  • 2021-12-03
  • 2021-10-18
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-03
  • 2022-01-12
  • 2022-12-23
  • 2021-05-19
  • 2022-12-23
  • 2021-04-27
相关资源
相似解决方案