【Redis】:Jedis 使用
Redis 支持很多语言, 例如C#,RUBY,JAVA 等, Jedis是redis的java版本的客户端实现
一个简单的Jedis使用
依赖第三方包
jedis-2.7.2.jar
commons-pool2-2.3.jar
JedisPoolManager 用户管理数据库连接,比如获取或者释放
package com.redis.jedis.project.common; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class JedisPoolManager { //private static String REDISDB_IP = "192.168.62.44"; private static String REDISDB_IP = "127.0.0.1"; private static int REDISDB_PORT = 6379; private JedisPool jedisPool; public JedisPoolManager() { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(20); config.setMaxIdle(5); config.setMaxWaitMillis(1000l); config.setTestOnBorrow(false); jedisPool = new JedisPool(config, REDISDB_IP, REDISDB_PORT); } public Jedis getJedis() throws Exception { try { Jedis jedis = jedisPool.getResource(); return jedis; } catch (Exception e) { throw e; } } public void releaseJedis(Jedis jedis) { if (jedis != null) { jedis.close(); } } }
JedisManager 用户Redis数据库操作,当前只写了增删改查等常用行为
package com.redis.jedis.project.common; import java.util.Map; import redis.clients.jedis.Jedis; public class JedisManager { private JedisPoolManager pool = new JedisPoolManager(); protected void set(int dbIndex, String key, String value, int cashSeconds) throws Exception { Jedis jedis = null; try { jedis = pool.getJedis(); jedis.select(dbIndex); jedis.set(key, value); if (cashSeconds > 0) { jedis.expire(key, cashSeconds); } } catch (Exception e) { throw e; } finally { pool.releaseJedis(jedis); } } protected String get(int dbIndex, String key) throws Exception { Jedis jedis = null; try { jedis = pool.getJedis(); jedis.select(dbIndex); return jedis.get(key); } catch (Exception e) { throw e; } finally { pool.releaseJedis(jedis); } } protected void delete(int dbIndex, String key) throws Exception { Jedis jedis = null; try { jedis = pool.getJedis(); jedis.select(dbIndex); jedis.del(key); } catch (Exception e) { throw e; } finally { pool.releaseJedis(jedis); } } protected Map<String, String> hgetAll(int dbIndex, String key) throws Exception { Jedis jedis = null; try { jedis = pool.getJedis(); jedis.select(dbIndex); return jedis.hgetAll(key); } catch (Exception e) { System.out.println(e); throw e; } finally { pool.releaseJedis(jedis); } } protected void hset(int dbIndex, String key, String field, String value) throws Exception { Jedis jedis = null; try { jedis = pool.getJedis(); jedis.select(dbIndex); jedis.hset(key, field, value); } catch (Exception e) { throw e; } finally { pool.releaseJedis(jedis); } } protected void hmset(int dbIndex, String key, Map<String, String> maps, int cacheSeconds) throws Exception { Jedis jedis = null; try { jedis = pool.getJedis(); jedis.select(dbIndex); jedis.hmset(key, maps); if (cacheSeconds >= 0) { jedis.expire(key, cacheSeconds); } } catch (Exception e) { throw e; } finally { pool.releaseJedis(jedis); } } protected void del(int dbIndex, String key) throws Exception { Jedis jedis = null; try { jedis = pool.getJedis(); jedis.select(dbIndex); jedis.del(key); } catch (Exception e) { throw e; } finally { pool.releaseJedis(jedis); } } }
第一个服务接口,实现具体业务
package com.redis.jedis.project.common; public interface ILoginCash { public boolean longin(LoginDTO loginDTO,int cashSeconds) throws Exception; public LoginDTO getLogInfo(int userId) throws Exception; public boolean longout(int userId) throws Exception; }
package com.redis.jedis.project.common; import java.io.Serializable; public class LoginDTO implements Serializable { private static final long serialVersionUID = 1L; private int userId; private String code; private String name; private String ip; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } }
实现服务接口时,同时继承JedisManager管理类,这样可以使用JedisManager封装的操作数据库的行为,为了更好的方便管理KEY名称,一般Rredis中的KEY 都是由 对象:ID 或者对象:ID:类型 等结构管理的
package com.redis.jedis.project.common; import java.util.HashMap; import java.util.Map; public class LoginCashManager extends JedisManager implements ILoginCash { private static final String REDIS_USER = "user"; @Override public boolean longin(LoginDTO loginDTO, int cashSeconds) throws Exception { boolean result = false; String key = REDIS_USER + ":" + loginDTO.getUserId(); try { Map<String, String> map = new HashMap<String, String>(); map.put("code", loginDTO.getCode()); map.put("name", loginDTO.getName()); map.put("ip", loginDTO.getIp()); hmset(RedisDBConstants.USER_DB_INDEX, key, map, cashSeconds); result = true; } catch (Exception e) { System.out.println(e); } return result; } @Override public LoginDTO getLogInfo(int userId) throws Exception { if (userId <= 0) { return null; } String key = REDIS_USER + ":" + userId; LoginDTO loginDTO = null; try { Map<String, String> map = hgetAll(RedisDBConstants.USER_DB_INDEX, key); if (map != null && !map.isEmpty()) { loginDTO = new LoginDTO(); loginDTO.setUserId(userId); loginDTO.setCode(map.get("code")); loginDTO.setName(map.get("name")); loginDTO.setIp(map.get("ip")); } } catch (Exception e) { System.out.println(e); } return loginDTO; } @Override public boolean longout(int userId) throws Exception { if (userId <= 0) { return false; } String tokenKey = REDIS_USER + ":" + userId; boolean result = false; try { del(RedisDBConstants.USER_DB_INDEX, tokenKey); result = true; } catch (Exception e) { System.out.println(e); } return result; } }
Redis默认有16个库,分别由数字 0到15编号,为方便管理,可以通过名称定义的方式以便查找
package com.redis.jedis.project.common; public class RedisDBConstants { public static int SYS_DB_INDEX = 0; public static int USER_DB_INDEX = 1; }
测试类
package com.redis.jedis.project.common; public class MainTestRedis { public static void main(String[] args) throws Exception { LoginCashManager logincash = new LoginCashManager(); System.out.println("==SAVE NOW=="); LoginDTO loginDTO = new LoginDTO(); loginDTO.setUserId(1000); loginDTO.setCode("1003"); loginDTO.setName("LIUY"); loginDTO.setIp("127.0.0.1"); logincash.longin(loginDTO,50); System.out.println("==SAVE END=="); LoginDTO login =logincash.getLogInfo(1000); System.out.println("==GET INFO=="); System.out.println("id="+login.getUserId()); System.out.println("code="+login.getCode()); System.out.println("name="+login.getName()); System.out.println("loginIP="+login.getIp()); } }
测试结果
==SAVE NOW==
==SAVE END==
==GET INFO==
id=1000
code=1003
name=LIUY
loginIP=127.0.0.1
不及格的飞鱼
http://www.cnblogs.com/liuyongcn/