day108-Cache-Distributed Lock-Redisson-Read-Write Lock

1. What is a read-write lock

Regarding read-write locks, as the name suggests, they are two types of locks that are used in read and write scenarios.

Read locks are shared locks and write locks are exclusive locks. What does it mean?

That is, write locks can only exist independently, and read locks can exist together

Generally speaking, in the read-write lock with the same name, when a write lock appears, the read lock with the same name cannot be locked, and the write lock with the same name cannot be locked.

When there is a read lock, the read lock with the same name can be locked, but the write lock with the same name cannot be locked.

2. For Shimo

In fact, this is all designed to comply with data consistency. Think about it,

When you write data to a table, another person is reading data to this table

Does he have to read the latest data after you finish writing the data? At this time, you can use a read-write lock to limit

Of course, if you do not have high requirements for data consistency in some of your businesses, then there is no need to add read-write locks

Of course, you are all reading, and you have to add a read lock at this time. If you read yours and I read mine, it will not affect you.

But while you are reading, another person acquires the write lock and prepares to write. Can it succeed?

Obviously it won’t work, because although the reading will be done first and the writing will be done here, there is no guarantee that you will finish reading there before writing to the cache or database.

If you write first, the other parties may read the data in the database before and after you write, so for consistency, it is reasonable to design the write lock as an exclusive lock.

3. Look at the example

Just give an example, here is only one scene

That is, before the write lock is released here, the read lock on the other side is blocked until the write lock is released, and the read lock can be successfully added to start reading.

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    StringRedisTemplate stringRedisTemplate;
   
    @RequestMapping({"/write"})
    @ResponseBody
    public void write(){

        //获取一把读写锁,只要名称一样就是同一把锁
        RReadWriteLock rwLock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = rwLock.writeLock();
        rLock.lock();
        try {
            Thread.sleep(30000);
            String uuid = UUID.randomUUID().toString();
            stringRedisTemplate.opsForValue().set("test-key",uuid);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            //释放锁
            rLock.unlock();
        }
    }

    @RequestMapping({"/read"})
    @ResponseBody
    public String read(){

        //获取一把读写锁,只要名称一样就是同一把锁
        RReadWriteLock rwLock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = rwLock.readLock();
        rLock.lock();
        String uuid = null;
        try {
             uuid = stringRedisTemplate.opsForValue().get("test-key");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //释放锁
            rLock.unlock();
            return uuid;
        }
    }

The phenomenon is that the window written above keeps rotating, and the window read below is also the window reading below. The write lock is released after the upper circle finishes writing, and the display below is displayed.

Guess you like

Origin blog.csdn.net/JavaCoder_juejue/article/details/115008478
Recommended