首页天道酬勤redis使用教程,redis工具类封装

redis使用教程,redis工具类封装

张世龙 05-05 19:12 18次浏览

packagejavax.utils; importjava.util.ArrayList; importjava.util.List; import redis.clients.jedis.jedis poolconfig; import redis.clients.jedis.jedisshardinfo; import redis.clients.jedis.shardedjedis; import redis.clients.jedis.shardedjedispool; /*** Redis工具类

*

*@authorLogan

* @版本1.0.0 * /

publicclassredisutils { privatestaticshardedjedispooljedispool=null; 静态{

jedispoolconfigjedispoolconfig=newjedispoolconfig (; //设置最大对象数

Jedispoolconfig.setmaxtotal(20; //可以保持空闲状态的最大对象数

Jedispoolconfig.setmaxidle(10; //超时时间

jedis poolconfig.setmaxwaitmillis (10000; //在获取连接时检查有效性,默认false

jedis poolconfig.settestonborrow (true; 返回对象时,对返回的连接进行验证对象检查

jedis poolconfig.settestonreturn (true; //集群的话可以全部放入list

列表共享信息=new ArrayList (;

jedisshardinfoshardinfo=newjedisshardinfo (' 192.168.32.10 ',6379 );

shard info.set password (' red is 123.';

shardinfos.add(shardinfo );

Jedi spool=newshardedjedispool (jedis poolconfig,shardInfos );

}/***从连接池获取ShardedJedis对象*/

publicstaticshardedjedisgetjedis { returnjedispool.get resource };

}/* * *关闭shardedjedis对象并将其放回池中*/

publicstaticvoidclosejedis (shardedjedisjedis ) {

jedis.close (;

通过}/***key获取字符串类型的值

*

* @ param密钥

* @返回值*

公共静态字符串获取(字符串密钥(try ) )。

shardedjedisjedis=Jedi spool.get resource (;

(returnJedis.get ) key;

}

从}/***key获取字节数组类型Value

*

* @ param密钥

* @返回值*

公共静态字节[ ]获取(字节[ ]密钥{try ) )。

shardedjedisjedis=Jedi spool.get resource (;

(returnJedis.get ) key;

}

}/* * *设置字符串类型密钥和值

*

* @ param密钥

*@paramvalue值

*@return

*/

publicstaticstringset (字符串密钥,字符串值({ try ) )。

shardedjedisjedis=Jedi spool.get resource (;

(return jedis.set (密钥,值);

}

}/***设置字节数组类型key和value

*

* @ param密钥

*@paramvalue值

*@return

*/

(publicstaticstringset (byte [ ] key,byte[] value ) (try ) ) ) ) ) ) 652

shardedjedisjedis=Jedi spool.get resource (;

(return jedis.set (密钥,值);

}

}/***指定的密钥* /

publicstaticlongdel (字符串密钥{try ) )。

shardedjedisjedis=Jedi spool.get resource (;

(returnJedis.del ) ) key;

}

(}/***在左侧加入集合

*

* @ param密钥

*@paramvalues值集合

*@return

*/

publicstaticlonglpush (字符串密钥,字符串. values ) try ) (

shardedjedisjedis=Jedi spool.get resource (;

(returnJedis.Lpush(key,values );

}

(}/***元素显示在左侧

*

*@paramkey指定键

*@return左侧的第一个元素*

publicstaticstringlpop (字符串密钥({ try ) )。

shardedjedisjedis=Jedi spool.get resource (;

(returnJedis.lpop ) key;

}

(}/***在右侧加入集合

*

* @ param密钥

*@paramvalues值集合

*@return

*/

publicstaticlongrpush (字符串密钥,字符串. values ({ try ) )。

shardedjedisjedis=Jedi spool.get resource (;

(returnJedis.rpush(key,values );

}

(}/***元素将显示在右侧

*

*@paramkey指定键

*@return右侧的第一个元素*

publicstaticstringrpop (字符串密钥({ try ) )。

shardedjedisjedis=Jedi spool.get resource (;

(returnJedis.rpop ) key;

}

}

}

,