文章目录
一、Redis是什么?
二、Redis的常用基本配置
三、Redis通用命令
四、Redis数据类型
1.String 字符串类型
2.Hash键值类型
3.List列表类型
4.Set与Zset集合
五、Jedis
1.maven导入依赖
@H_502_123@<dependencies> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> </dependencies>
2.使用
@H_502_123@public class JedisTestor { public static void main(String[] args) { Jedis jedis = new Jedis("192.168.86.128",6379); try{ jedis.auth("123456"); jedis.select(2); System.out.println("success"); jedis.mset(new String[]{"title","奶粉","num","20"}); List<String> mget = jedis.mget(new String[]{"sn", "title", "num"}); System.out.println(mget); Long num = jedis.incr("num"); System.out.println(num); jedis.hset("student:3312","name","小明"); String name = jedis.hget("student:3312", "name"); System.out.println(name); Map<String,String> stu = new HashMap(); stu.put("name","lili"); stu.put("age","18"); stu.put("id","3313"); jedis.hmset("student:3313",stu); Map<String, String> smap = jedis.hgetAll("student:3313"); System.out.println(smap); jedis.del("letter"); jedis.rpush("letter",new String[]{"d","e","f"}); jedis.lpush("letter",new String[]{"c","b","a"}); List<String> letter = jedis.lrange("letter", 0, -1); jedis.lpop("letter"); jedis.rpop("letter"); letter = jedis.lrange("letter", 0, -1); System.out.println(letter); }catch (Exception e){ e.printstacktrace(); }finally { jedis.close(); } } }
3.Jedis缓存数据
@H_502_123@public class CacheSample { public CacheSample(){ Jedis jedis = new Jedis("192.168.86.128",6379); try { List<Goods> goodsList = new ArrayList<>(); goodsList.add(new Goods(8818,"pg","",3.5f)); goodsList.add(new Goods(8819,"xj","",5f)); goodsList.add(new Goods(8820,"cz","",25f)); jedis.auth("123456"); jedis.select(3); for(Goods goods:goodsList){ String json = JSON.toJSONString(goods); System.out.println(json); String key = "goods"+goods.getGoodsId(); jedis.set(key,json); } }catch (Exception e){ e.printstacktrace(); }finally { jedis.close(); } } public static void main(String[] args) { new CacheSample(); System.out.println("输入要查询的商品编号"); String goodsId = new Scanner(system.in).next(); Jedis jedis = new Jedis("192.168.86.128",6379); try{ jedis.auth("123456"); jedis.select(3); String key = "goods"+goodsId; if(jedis.exists(key)){ String s = jedis.get(key); System.out.println(s); Goods g = JSON.parSEObject(s, Goods.class); System.out.println(g.getGoodsName()); }else{ System.out.println("编号错误"); } }catch (Exception e){ e.printstacktrace(); }finally { jedis.close(); } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。