redis问题有哪些
这篇文章主要为大家展示了“redis问题有哪些”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“redis问题有哪些”这篇文章吧。
我们提供的服务有:成都网站建设、成都网站制作、微信公众号开发、网站优化、网站认证、千山ssl等。为上千余家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的千山网站制作公司
redis的线程模型:redis是单线程模型,使用epoll多路复用器实现,文件事件处理,然后文件事件分发
redis数据类型:String、List、Hash、Set、Zset
redisTemplate.opsForValue();//操作字符串
redisTemplate.opsForHash();//操作hash
redisTemplate.opsForList();//操作list
redisTemplate.opsForSet();//操作set
redisTemplate.opsForZSet();//操作有序set
String
redisTemplate.opsForValue().set("num","123");
redisTemplate.opsForValue().get("num")
输出结果为123
redisTemplate.opsForValue().set("num","123",10, TimeUnit.SECONDS);
redisTemplate.opsForValue().get("num")设置的是10秒失效,十秒之内查询有结果,十秒之后返回为null
template.opsForValue().getAndSet("getSetTest","test2")//设置键的字符串值并返回其旧值
template.opsForValue().size("key")// 返回key所对应的value值得长度
Hash
删除给定的哈希hashKeys
template.opsForHash().delete("redisHash","name")//结果:1
template.opsForHash().entries("redisHash")//结果:{class=6, age=28.1}
确定哈希hashKey是否存在
template.opsForHash().hasKey("redisHash","666")//结果:true
template.opsForHash().hasKey("redisHash","777")//结果:false
从键中的哈希获取给定hashKey的值
template.opsForHash().get("redisHash","age");//结果:26
获取key所对应的散列表的key,redisHash所对应的散列表为{class=1, name=666, age=27}
template.opsForHash().keys("redisHash")//结果:[name, class, age]
设置散列hashKey的值
template.opsForHash().put("redisHash","name","666");
template.opsForHash().put("redisHash","age",26);
template.opsForHash().put("redisHash","class","6");
template.opsForHash().entries("redisHash")//结果:{age=26, class=6, name=666}
获取整个哈希存储的值根据密钥
template.opsForHash().values("redisHash");//结果:[tom, 26, 6]
List
将所有指定的值插入存储在键的列表的头部
template.opsForList().leftPush("list","java");
批量把一个数组插入到列表中
String[] strs = new String[]{"1","2","3"};
template.opsForList().leftPushAll("list",strs);
template.opsForList().range("list",0,-1)//结果:[3, 2, 1]
void set(K key, long index, V value);
在列表中index的位置设置value值
template.opsForList().range("listRight",0,-1)//结果:[java, python, oc, c++]
template.opsForList().set("listRight",1,"setValue");
template.opsForList().range("listRight",0,-1)//结果:[java, setValue, oc, c++]
从存储在键中的列表中删除等于值的元素的第一个计数事件。
计数参数以下列方式影响操作:
count> 0:删除等于从头到尾移动的值的元素。
count <0:删除等于从尾到头移动的值的元素。
count = 0:删除等于value的所有元素。
template.opsForList().range("listRight",0,-1)//结果:[java, setValue, oc, c++]
template.opsForList().remove("listRight",1,"setValue");//将删除列表中存储的列表中第一次次出现的“setValue”。
template.opsForList().range("listRight",0,-1)//结果:[java, oc, c++]
根据下表获取列表中的值,下标是从0开始的
template.opsForList().range("listRight",0,-1)//结果:[java, oc, c++]
template.opsForList().index("listRight",2)//结果:c++
弹出最左边的元素,弹出之后该值在列表中将不复存在
template.opsForList().range("list",0,-1)//结果:[c++, python, oc, java, c#, c#]
template.opsForList().leftPop("list")//结果:c++
template.opsForList().range("list",0,-1)//结果:[python, oc, java, c#, c#]
Set
无序集合中添加元素,返回添加个数
也可以直接在add里面添加多个值 如:template.opsForSet().add(“setTest”,“aaa”,“bbb”)
String[] strs= new String[]{"str1","str2"};
template.opsForSet().add("setTest", strs)//结果:2
移除集合中一个或多个成员
String[] strs = new String[]{"str1","str2"};
template.opsForSet().remove("setTest",strs);//结果:2
移除并返回集合中的一个随机元素
template.opsForSet().pop("setTest");//结果:bbb
template.opsForSet().members("setTest");//结果:[aaa, ccc]
将 member 元素从 source 集合移动到 destination 集合
template.opsForSet().move("setTest","aaa","setTest2");
template.opsForSet().members("setTest")//结果:[ccc]
template.opsForSet().members("setTest2")System.out.println();//结果:[aaa]
返回集合中的所有成员
template.opsForSet().members("setTest")//结果;[ddd, bbb, aaa, ccc]
ZSet
新增一个有序集合,存在的话为false,不存在的话为true
template.opsForZSet().add("zset1","zset-1",1.0);//结果:true
新增一个有序集合
ZSetOperations.TypedTuple