当前位置:网站首页>Redis and jedis
Redis and jedis
2022-06-25 01:20:00 【Lutrra】
thing
#redis A single command is atomic, but things don't
#redis The essence of things : A collection of commands , All commands in a thing are serialized , During the execution of things , Execute in order
#reids Things have no isolation level
# All commands are in things , Not directly executed , Only when you initiate an execution command will you execute
# Opening things
multi
# Order to join the team
# Execute things
exec
# Normal things
multi
set k1 v1
set k2 v2
exec
# Execute sequentially End of execution
# Cancel something
DISCARD
# Once you give up , All will not be executed
# error
#1. Compile exception
# Will not execute
multi
set k1 v1
set k2 v2
getset k3
set k4 v4
exec
#2. Abnormal operation
# There is a grammatical error , Other commands can be used
multi
set k1 "v1"
incr k1
set k2 v2
get k2
exec
monitor
Pessimistic locking
# I think there will be problems all the time , It will be locked at any time
Optimism lock
# Think there will be no problem at all , So it won't lock , Judge when updating data , Has anyone modified the data during this period
# obtain version
set money 100
set out 0
watch money # Monitored object
multi # Things end normally , There was no change during the data period , This time, the implementation will be successful
DECRBY money
INCRBY out 20
exec
# Thread one
watch money
multi
decrby money 10
incrby out 10
# Threads 2 Make changes
set money 1000
# Thread one
exec # error
unwatch
watch money
multi
decrby money 10
incrby out 10
jedis
<!-- redis Recommended by the official website java Connect development tools ! Use java Middleware for operation Import corresponding dependency -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
Test local connection
pulic class TestPing{
public static void main(string[] args){
Jedis jedis= new Jedis("127.0.0.1",6379);
//jedis All commands are redis All the orders of
system.out.println(jedis.ping())
}
}
adopt jedis Understand things
pulic class TestPing{
public static void main(string[] args){
Jedis jedis= new Jedis("127.0.0.1",6379);
//jedis All commands are redis All the orders of
JSONObject jsaonobject =new JSONObject();
jsonobject.put("hello","world");
Transation multi=jedis.multi();
String result=jsonobject.toJSONString();
try{
multi.set("user1",result);
multi.exec();
}
catch{
multi.discard();
e.printStackTrace();
}finally{
jedis.close();// Close the connection
}
}
}
springboot Integrate
#springboot Do not use jedis Connect , use lettuce
spring.redis.host="127.0.0.1"
spring.redis.port=6379
@Autowired
private RedisTemplate redisTemplate;
void contextLoads(){
redis.opForValue().set("mykey","lutrra");
system.out.println(redisTemplate.opForValue.get("mykey"));
}
边栏推荐
猜你喜欢

Abnova丨A4GNT多克隆抗体中英文说明

Reading notes at night -- deep into virtual function

Bi-sql select into

LLVM TargetPassConfig

利用 Redis 的 sorted set 做每周热评的功能
The latest QQ wechat domain name anti red PHP program source code + forced jump to open

Install mysql5.6 under linux64bit - the root password cannot be modified

Tianshu night reading notes -- memory paging mechanism

Bi-sql index

汇编语言(4)函数传参
随机推荐
利用 Redis 的 sorted set 做每周热评的功能
void* 指针
[practical series] full WiFi coverage at home
Using bindservice method to pause music playing
联想童夫尧:11倍于大势,我们一路攻城拔寨
4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
Powerbi - for you who are learning
Assembly language (4) function transfer parameters
Distinguish between i++ and ++i seconds
程序员:是花光积蓄在深圳买房?还是回到长沙过“富余”生活?
Abnova丨CSV 磁珠中英文说明
Ideas and examples of divide and conquer
15. several methods of thread synchronization
Tencent moved!
Bi SQL alias
实验5 8254定时/计数器应用实验【微机原理】【实验】
Picture rotation move zoom gradient
Transform BeanUtils to achieve list data copy gracefully
Cobalt strike installation tutorial
新一代可级联的以太网远程I/O数据采集模块