javaWeb how to prevent the simultaneous operation of two of the same record (single operation limit), the use of solutions redis

Recently doing demo, encounter such a problem: when the page has the same role when two people log on, they operate simultaneously unified records; in short, the same record more than one time operation. This situation leads to my business back redundant data, interfere with the normal operation of the program.

Then think of solutions to do is lock the cache table or use (of course, other ways to explore - leave a comment below), lock table have not tried so naturally think of cache.
The idea is to achieve, when the end user selects the recording page and operates in the server to service processing is not performed, but the first use of the cache, the operation determines whether the service information present in the single cache, there is a direct return to the page Ruoguo end, prompting the user to the current record being processed; if not performed after the operation information stored in the new piece of service, (custom format of operation information, uniqueness noted), then business logic processing, the processing is completed (including an abnormal) deleting the cache information here on the big announcement percent.
(If it is a distributed cluster, pay attention to cache shared ~~)
initial attempt, to welcome all of you correct me!
The following is a part of the contents of the code:

public boolean doRedisLock(List<String> idList,String LockFlag,String cacheName){
        boolean islockSuccess = true;
        //防止多人人重复操作
        Map cache = CacheManager.getCache(cacheName);

        List<String> tempLockIdsList = new ArrayList<>();
        for (String idTemp : idList) {
            //判断当前记录是否已经正在办理或者已经办理
            String nowStatus = (String) cache.get(idTemp+LockFlag);
            if(nowStatus == null){//如果当前的缓存中存在该记录的操作,继续遍历
                tempLockIdsList.add(idTemp);
                //先将已经遍历的未被他人操作的记录进行锁定,期间不准他人操作
                cache.put(idTemp+cacheName,cacheName);//先将该记录的业务新增至缓存
            }else{//存在已经在办理的记录
                //将预先锁定的记录解除锁定,清除缓存
                for(String tempLockId: tempLockIdsList){
                    cache.remove(tempLockId+cacheName);
                }
                islockSuccess = false ;
            }

        }
        return islockSuccess;
    }
Published 37 original articles · won praise 29 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_42755868/article/details/89354264