当前位置:网站首页>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"));
}
原网站

版权声明
本文为[Lutrra]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210541309499.html