关于Redis一些资料的学习可以到http://www.linuxidc.com/search.aspx?Where=Nkey&Keyword=Redis去学习 ,很全面
下面介绍一个简单Java客户端Jedis,大家可以到https://github.com/xetorthio/jedis这网址下载
这里给大家提供一个简单的对jedis的封装类以供参考
Redis.java
- package com.ajun.redis;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisPool;
- import redis.clients.jedis.JedisPoolConfig;
- /**
- *
- * @author ajun
- *
- */
- public class Redis {
- private static JedisPool pool;
- private static int DBIndex;
- private static String host;
- private static int port=6379;
- private static int timeout=60*1000;
- static {
- DBIndex=Integer.parseInt(PubConstant.getConfigureValue("redis_dbindex"));
- host=PubConstant.getConfigureValue("redis_host");
- JedisPoolConfig config = new JedisPoolConfig();
- config.setMaxActive(100);
- config.setMaxIdle(20);
- config.setMaxWait((long)1000);
- config.setTestOnBorrow(false);
- pool = new JedisPool(config, host, port, timeout);//线程数量限制,IP地址,端口,超时时间
- }
- public static void addItemToList(String key,byte[] value)
- {
- Jedis jedis=null;
- try {
- jedis = pool.getResource();
- jedis.connect();
- jedis.select(DBIndex);
- jedis.lpush(key.getBytes(), value);
- } catch (Exception e) {
- e.printStackTrace();
- }
- finally{
- if(jedis!=null)
- pool.returnResource(jedis);
- }
- }
- @SuppressWarnings("finally")
- public static List<String> getItemFromList(String key)
- {
- Jedis jedis=null;
- //byte[] s=null;
- List<String> ss=null;
- try {
- jedis = pool.getResource();
- jedis.select(DBIndex);
- long len=jedis.llen(key);
- if(len==0) return null;
- ss = jedis.lrange(key, 0, (int)len);
- } catch (Exception e) {
- e.printStackTrace();
- }
- finally{
- if(jedis!=null)
- pool.returnResource(jedis);
- return ss;
- }
- }
- public static void addItem(String key,byte[] value)
- {
- Jedis jedis=null;
- try {
- jedis = pool.getResource();
- jedis.select(DBIndex);
- jedis.set(key.getBytes(), value);
- } catch (Exception e) {
- e.printStackTrace();
- }
- finally{
- if(jedis!=null)
- pool.returnResource(jedis);
- }
- }
- public static byte[] getItem(String key)
- {
- Jedis jedis=null;
- byte[] s=null;
- try {
- jedis = pool.getResource();
- jedis.select(DBIndex);
- s = jedis.get(key.getBytes());
- return s;
- } catch (Exception e) {
- e.printStackTrace();
- return s;
- }
- finally{
- if(jedis!=null)
- pool.returnResource(jedis);
- }
- }
- public static void delItem(String key)
- {
- Jedis jedis=null;
- try {
- jedis = pool.getResource();
- jedis.select(DBIndex);
- jedis.del(key.getBytes());
- } catch (Exception e) {
- e.printStackTrace();
- }
- finally{
- if(jedis!=null)
- pool.returnResource(jedis);
- }
- }
- public static long getIncrement(String key)
- {
- Jedis jedis=null;
- try {
- jedis = pool.getResource();
- jedis.select(DBIndex);
- return jedis.incr(key);
- } catch (Exception e) {
- e.printStackTrace();
- return 0L;
- }
- finally{
- if(jedis!=null)
- pool.returnResource(jedis);
- }
- }
- /**
- * 设置map 可以存储用户信息
- * @param key
- * @param map
- */
- public static void setHashMap(String key,HashMap<String,String> map){
- Jedis jedis=null;
- try {
- jedis = pool.getResource();
- jedis.select(DBIndex);
- if(map!=null && !map.isEmpty()){
- for(Map.Entry<String, String> entry : map.entrySet()){
- jedis.hset(key, entry.getKey(), entry.getValue());
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- if(jedis!=null)
- pool.returnResource(jedis);
- }
- }
- public static Map<String,String> getHashMap(String key){
- Map<String,String> map = new HashMap<String,String>();
- Jedis jedis=null;
- try {
- jedis = pool.getResource();
- jedis.select(DBIndex);
- map = jedis.hgetAll(key);
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- if(jedis!=null)
- pool.returnResource(jedis);
- }
- return map;
- }
- /**
- * 添加set
- * @param key
- * @param set
- */
- public static void addSet(String key,Set<String> set){
- Jedis jedis=null;
- try {
- jedis = pool.getResource();
- jedis.select(DBIndex);
- if(set!=null && !set.isEmpty()){
- for(String value : set){
- /*for ( Iterator<String> memberItr =
- jedis.smembers(str).iterator();//返回key对应set的所有元素,结果是无序的
- memberItr.hasNext();){
- final String member = memberItr.next();
- if (!jedis.sismember(str, member)){
- jedis.srem(str, member);
- }
- }*/
- jedis.sadd(key, value);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- if(jedis!=null)
- pool.returnResource(jedis);
- }
- }
- public static Set<String> getSet(String key){
- Set<String> sets = new HashSet<String>();
- Jedis jedis=null;
- try {
- jedis = pool.getResource();
- jedis.select(DBIndex);
- sets = jedis.smembers(key);
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- if(jedis!=null)
- pool.returnResource(jedis);
- }
- return sets;
- }
- }
