Redis
2021/10/25 2:13:31
本文主要是介绍Redis,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
文章目录
- 一、Redis是什么?
- 二、Redis的常用基本配置
- 三、Redis通用命令
- 四、Redis数据类型
- 1.String 字符串类型
- 2.Hash键值类型
- 3.List列表类型
- 4.Set与Zset集合
- 五、Jedis
- 1.maven导入依赖
- 2.使用
- 3.Jedis缓存数据
一、Redis是什么?
二、Redis的常用基本配置
三、Redis通用命令
四、Redis数据类型
1.String 字符串类型
2.Hash键值类型
3.List列表类型
4.Set与Zset集合
五、Jedis
1.maven导入依赖
<dependencies> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> </dependencies>
2.使用
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缓存数据
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(); } } }
这篇关于Redis的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-08阿里云Redis项目实战入门教程
- 2024-11-08阿里云Redis资料:新手入门与初级使用指南
- 2024-11-08阿里云Redis教程:新手入门及实用指南
- 2024-11-07阿里云Redis学习入门:新手必读指南
- 2024-11-07阿里云Redis学习入门:从零开始的操作指南
- 2024-11-07阿里云Redis学习:初学者指南
- 2024-11-06阿里云Redis入门教程:轻松搭建与使用指南
- 2024-11-02Redis项目实战:新手入门教程
- 2024-10-22Redis入门教程:轻松掌握数据存储与操作
- 2024-10-22Redis缓存入门教程:快速掌握Redis缓存基础知识