当前位置:网站首页>狂神redis笔记09
狂神redis笔记09
2022-07-22 18:13:00 【抹茶味的西瓜汁】

开启事务 multi:
命令入队就是写正常的其它命令
执行事务是exec
正常执行事务!

放弃事务:
我们每次事务执行完毕就结束了,下次想使用事务还要重新开启:
编译性异常(代码有问题!命令有错!)事务中所有的命令都不会被执行!

运行时异常(1/0),如果事务队列中存在语法性错误,那么执行命令的时候,其它命令是可以正常执行的,错误命令抛出异常!
监控!

监视测试:

我们监视我们的money,正常执行成功是这样的。
测试多线程修改值,使用watch可以当做redis的乐观锁操作!
我们再开一个服务端
我们的线程2取出10块,还没有执行事务。
这时候线程1又去把余额充值到1000了:

我们在执行之前,另外一个事务修改值,我们执行就失败了!
那我们能不能去解决这个问题呢?

如果修改失败,获取最新的值就好。
unwatch:如果发现事务执行失败,就先解锁。
watch:获取最新的值,再次监视,select version
exec:比对监视的值是否发生了变化,如果没有变化,那么可以执行成功,如果变化就执行失败。重复这个操作就可以了。
面试的时候redis经常问乐观锁。

我们去创建一个java空项目:
里面创建一个module。
首先我们要去导入我们的jedis的包:

编码测试:
连接数据库
操作命令
断开连接
我们这里是启动了我们本地的redis:
常用的Api我们直接看一下就行和之前的命令都差不多:
这里我就不去自己写代码了,直接截屏狂神的代码。
我们现在来用jedis做一个正常的事务:

我们这里有残余是没有清空缓存。
我们加上flushdb:
我们现在就可以使用我们的java去做所有的我们redis的功能了。下一次课我们会去在springboot里去整合我们的redis.
边栏推荐
猜你喜欢

Unix programming project - the client based on raspberry pie regularly obtains the temperature and reports it to the server

Chapter5 深度学习基础

LC:剑指 Offer 05. 替换空格

pwn栈溢出基础练习题——2

ESP IDF vscode configuration from downloading tool chain to creating project, step record

C语言知识点(指针知识类型)

Chapter6 卷积神经网络(CNN)

Input a string of characters from the keyboard, output different characters and the number of times each character appears. (the output is not in order) use the common methods of string class to solve

Over fitting weight regularization and dropout regularization

The simplest scull device driver
随机推荐
Configure the private chirpstack of lorawan in the LAN
A rough understanding of firewall
[强网杯 2019]随便注
Introduction to SQL -- Basic additions, deletions, modifications, and exercises
Fiddler script personalized configuration display
2019_ ACL_ Multimodal Transformer for Unaligned Multimodal Language Sequences
[untitled]
【数据库连接】——节选自培训
Lc: sword finger offer 10- I. Fibonacci series
2020_ACM MM_MISA: Modality-Invariant and -Specific Representations for Multimodal Sentiment Analysis
SQL入门学习--基本增删改查以及练习
2019_AAAI_Multi-Interactive Memory Network for Aspect Based Multimodal Sentiment Analysis
Transplantation de systèmes embarqués
Saisissez une chaîne de caractères à partir du clavier et affichez différents caractères et le nombre d'occurrences de chaque caractère. (la sortie n'est pas séquentielle) résoudre le problème en util
BUUCTF 杂项——二维码
文件目录权限
Firewall knowledge, principle, equipment, manufacturer research summary report
Implementing IO multiplexing in UNIX using poll function to realize network socket server
Input a string of characters from the keyboard, output different characters and the number of times each character appears. (the output is not in order) use the common methods of string class to solve
PWN —— ret2libc1