Jedis操作Redis模型
来源:互联网 发布:淘宝网如何绑定银行卡 编辑:程序博客网 时间:2024/06/06 08:26
对于外部程序,想要访问Redis首先需要对Redis进行两个设置
在Redis配置文件redis.conf中配置如下
1. 注释掉bang 127.0.0.1,否则只能本机访问 2. 关闭redis的保护模式
不推荐使用new jedis
这样的操作,因为操作一次都要对redis进行远程访问,会大量的消耗资源
推荐使用连接池,其中有三个非常重要的类
JedisPoolConfig:设置和读取配置文件JedisPool:Jedis连接池Jedis:Jedis操作对象
首先先来一个new jedis的栗子
package jedisdemo;import redis.clients.jedis.Jedis;/** * Jedis 操作 Redis */public class JedisTest { private static Jedis jedis; public static void main(String[] args) { jedis = new Jedis("192.168.242.161", 6379); jedis.set("name", "zhangsan"); String value = jedis.get("name"); System.out.println("name = " + value); }}
Console输出name = zhangsan
下面是使用连接池的栗子
package jedisdemo;import static org.junit.Assert.*;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;import org.junit.After;import org.junit.Before;import org.junit.Test;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;/** * Jedis 操作 Redis(使用连接池) */public class JedisPoolTest { private static int MAX_TOTAL = 20; // 最大连接数,默认为8,-1表示不限制 private static int MAX_IDLE = 10; // 最大空闲连接数,默认为8 private static int MIN_IDLE = 5; // 初始化连接数 private static int MAX_WAIT = 10000; // 最大等待时间,单位为毫秒,默认-1,永不超时,如果超时,抛出异常 JedisConnectionException private static boolean TEST_ON_BORROW = true; // 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的; private static String HOST = "192.168.242.161"; // IP private static int PORT = 6379; // 端口,默认6379 private static String AUTH = "admin"; // redis 密码 private static int TIMEOUT = 1000; // 超时时间 private static Jedis jedis = null; private JedisPool jedisPool = null; @Before public void init() throws Exception { JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxTotal(MAX_TOTAL); poolConfig.setMaxIdle(MAX_IDLE); poolConfig.setMinIdle(MIN_IDLE); poolConfig.setMaxWaitMillis(MAX_WAIT); poolConfig.setTestOnBorrow(TEST_ON_BORROW); // 如果有密码,需要设置 // JedisPool jedisPool = new JedisPool(poolConfig, HOST, PORT, TIMEOUT, AUTH); jedisPool = new JedisPool(poolConfig, HOST, PORT); jedis = jedisPool .getResource(); } // Redis String 字符串 @Test public void testString() throws Exception { // set get jedis.set("address", "HeBei"); System.out.println("address = " + jedis.get("address")); // append jedis.append("address", " tangsan"); System.out.println("address = " + jedis.get("address")); // del jedis.del("address"); System.out.println("address = " + jedis.get("address")); } // Redis List 列表 @Test public void testList() throws Exception { // lpush lrange jedis.lpush("dblist", "mysql", "oracle", "db2"); System.out.println("dblist = " + jedis.lrange("dblist", 0, -1)); // rpush lrange jedis.rpush("dblist", "mongodb", "redis"); System.out.println("dblist = " + jedis.lrange("dblist", 0, -1)); // lpop lrange jedis.lpop("dblist"); System.out.println("dblist = " + jedis.lrange("dblist", 0, -1)); // llen System.out.println(jedis.llen("dblist")); } // Redis Set 无序集合 @Test public void testSet() throws Exception { // sadd smembers jedis.sadd("myset", "peach", "pear", "banana"); System.out.println("myset = " + jedis.smembers("myset")); // srem smembers jedis.srem("myset", "banana"); System.out.println("myset = " + jedis.smembers("myset")); // scard System.out.println(jedis.scard("myset")); } // Redis Hash 哈希 @Test public void testHash() throws Exception { // hset Map<String, String> hash = new HashMap<String, String>(); hash.put("name", "tom"); hash.put("age", "20"); hash.put("address", "ShangHai"); jedis.hmset("user", hash); // hdel jedis.hdel("user", "address"); // hexists System.out.println(jedis.hexists("user", "address")); // hkeys Set<String> hkeys = jedis.hkeys("user"); for (String str : hkeys) { System.out.println(str); } // hvals List<String> hvals = jedis.hvals("user"); for (String str : hvals) { System.out.println(str); } // hgetall Map<String, String> map = jedis.hgetAll("user"); for(Map.Entry<String, String> entry : map.entrySet()) { System.out.println(entry.getKey() + " = " + entry.getValue()); } } // Redis ZSet 有序集合 @Test public void testZSet() throws Exception { // zadd zrange Map<String, Double> scoreMembers = new HashMap<String, Double>(); scoreMembers.put("math", 90.0); scoreMembers.put("english", 87.5); scoreMembers.put("sports", 94.0); scoreMembers.put("music", 67.0); scoreMembers.put("politics", 97.0); jedis.zadd("score_set", scoreMembers); System.out.println(jedis.zrange("score_set", 0, -1)); System.out.println(jedis.zrangeWithScores("score_set", 0, -1)); // zrem jedis.zrem("score_set", "sports"); System.out.println(jedis.zrange("score_set", 0, -1)); // zcard System.out.println(jedis.zcard("score_set")); // zrank System.out.println(jedis.zrank("score_set", "math")); } // Java对象存储到Redis(使用序列化) @Test public void testSerialize() throws Exception { User user = new User(1, "张三", "河北"); jedis.set("user".getBytes(), SerializeUtil.serialize(user)); } // Redis 反序列化后转为Java对象 @Test public void testDeSerialize() throws Exception { byte[] bytes = jedis.get("user".getBytes()); System.out.println((User)SerializeUtil.deSerialize(bytes)); } // 释放资源 @After public void destory() throws Exception { if (jedis != null) { jedis.close(); } }}
序列化和反序列化工具类
package jedistest;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;public class SerializeUtil { /** * 序列化对象 */ public static byte[] serialize(Object obj) { byte[] bytes = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ; ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(obj); bytes = baos.toByteArray(); baos.close(); oos.close(); } catch (IOException e) { e.printStackTrace(); } return bytes; } /** * 反序列化对象 */ public static Object deSerialize(byte[] bytes) { Object obj = null; try { ByteArrayInputStream bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bais); obj = ois.readObject(); } catch (Exception e) { e.printStackTrace(); } return obj; }}
阅读全文
0 0
- Jedis操作Redis模型
- redis 之 jedis操作
- Jedis操作redis
- 使用Jedis操作redis
- Jedis操作redis
- jedis 操作redis集合
- 使用Jedis操作Redis
- 使用Jedis操作redis
- Jedis操作redis
- Jedis操作redis
- Java操作Redis--Jedis
- jedis-JAVA操作Redis
- jedis操作redis
- Jedis操作Redis
- Jedis操作Redis
- jedis操作redis
- 使用Jedis操作Redis
- jedis操作redis
- 坚持#第191天~好了!注意了!人生的转折点!梦想的启航!云计算的开始!
- ReadMe
- 对字符串数组中的单词逆序输出
- Python cookbook进阶学习-元编程(一)
- hdu2255 二分图最佳匹配
- Jedis操作Redis模型
- 第三节循环语句和函数初识
- IEEE论文检测的字体未嵌入问题Times New Roman,Bold, Times New Roman,Italic is not embedded解决方法
- 设计模式从零开始——简单工厂模式
- 9.23【NOIP模拟总结】
- ReadMe
- XTU C语言程序设计实践作业4
- ele嘛~~~~~~~~~~~~~~~~~~
- 第四节初识面向对象及eclipse快捷键