redis 起步

本文介绍了一个基于Redis连接池的实用工具类实现。通过配置文件设置连接参数,利用JedisPool进行资源管理,确保高效地复用连接并减少系统开销。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

刚学习redis,所以想写了一个redis底层帮助类,思想如下:

/**
	 *  (1)先配置redis.properties里面写配置相关信息
		(2)写一个RedisUtil里面满足如下条件
		a:能连接redis用到单例, 并且是从线程池里拿
		b:有各种自己所需要的方法(这个根据自己的需要来添加)
	 * 
	 * */
<pre name="code" class="java">  /**<pre name="code" class="java">redis.properties
*/

  redis.pool.maxActive=1024  
    redis.pool.maxIdle=200  
    redis.pool.maxWait=1000  
    redis.pool.testOnBorrow=true  
    redis.pool.testOnReturn=true  
    #IP  
    redis.ip=10.11.20.140  
    #Port  
    redis.port=6379  



<pre name="code" class="java">/**<pre name="code" class="java"><pre name="code" class="java">RedisUtil

*/

<pre name="code" class="java">package com.yc.dao;


import java.util.List;
import java.util.ResourceBundle;

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

public class RedisUtil {
	/**
	 *  (1)先配置redis.properties里面写配置相关信息
		(2)写一个RedisUtil里面满足如下条件
		a:能连接redis用到单例, 并且是从线程池里拿
		b:有各种自己所需要的方法(这个根据自己的需要来添加)
	 * 
	 * */
	 /***************池化使用jedis********************************/
    private static JedisPool pool;  
    static {  
        ResourceBundle bundle = ResourceBundle.getBundle("redis");  
        if (bundle == null) {  
            throw new IllegalArgumentException(  
                    "[redis.properties] is not found!");  
        }  
        JedisPoolConfig config = new JedisPoolConfig();  
        config.setMaxTotal(Integer.valueOf(bundle.getString("redis.pool.maxActive")));//最大连接数
        config.setMaxWaitMillis(Long.valueOf(bundle.getString("redis.pool.maxWait")));//最大等待时间
     //   config.setMaxActive(Integer.valueOf(bundle.getString("redis.pool.maxActive")));  //版本
        config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));  
    //    config.setMaxWait(Long.valueOf(bundle.getString("redis.pool.maxWait"))); //版本
        config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow")));  
        config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn")));  
        pool = new JedisPool(config, bundle.getString("redis.ip"),  
                Integer.valueOf(bundle.getString("redis.port")));  
    }  
    
    
    
    /***
     * 从池中获取一个Jedis对象
     */	
    public Jedis getJedis() {  
        return pool.getResource();  
    }  
    
    /** 
     * 从连接池中释放jedis 
     *  
     * @param jd 
     */  
    public void releaseJedis(Jedis jd) {  
    	pool.returnResource(jd);  
        jd = null;  
    }  
    
    
    
    public String setString(String key, String value) {  
        Jedis jedis = this.pool.getResource();  
        String status = jedis.set(key, value);  
        this.pool.returnResource(jedis);  
        return status;  
    }  
    
    
    public String getString(String key) {  
    	Jedis jedis = this.pool.getResource();  
        String value = jedis.get(key);  
        this.pool.returnResource(jedis);  
        return value;  
    }  
    
    /** 
     * This Stirng的批量操作 
     * @param pairs 
     */  
    
    public String set(byte[] key, byte[] value) {
    	  Jedis jedis = this.pool.getResource();  
          String status = jedis.set(key, value);  
          this.pool.returnResource(jedis);  
          return status; 
    }
    
    public byte[] get(byte[] key) {
    	Jedis jedis = this.pool.getResource();  
        byte[] value = jedis.get(key);  
        this.pool.returnResource(jedis);  
        return value;
   }
    
    
    /**
     * 删除操作
     * */
    
    public Long del(String key) {  
        Jedis jedis = this.pool.getResource();  
        Long status = jedis.del(key);  
        this.pool.returnResource(jedis);  
        return status;  
    }  
    
    
    
    
    
}





//这是最早的版本,我还会更新

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值