代码之家  ›  专栏  ›  技术社区  ›  We are Borg

Spring MVC:编辑5分钟后过期特权标志

  •  0
  • We are Borg  · 技术社区  · 11 年前
    • 我正在开发一个Spring MVC应用程序 喜欢给用户在模态内编辑的权限,仅限5 分钟我为此设置了一个布尔标志,所以如果 多人希望编辑此字段,然后他们可以阅读某人 正在编辑,并将自行承担编辑风险。
      • 但我的问题是,如果第一个用户断电或 断开连接,我想再次在数据库中设置注释 可编辑,通过使状态为假。所以,我想允许编辑标志 持续5分钟,然后停用它。我如何实现这一点?这是我的 密码

    :

      @Override
            public void setActiveEdit(int noteId, boolean status) {
                GroupNotes groupNotes = this.groupNotesDAO.getGroupNoteById(noteId);
        // Start a 5 minutes counter here if status is true
                groupNotes.setActiveEdit(status);
                this.groupNotesDAO.editGroupNote(groupNotes,groupNotes.getOwnednotes().getMsectionid());
            }
    

    任何帮助都很好。非常感谢。:-)

    3 回复  |  直到 11 年前
        1
  •  1
  •   Ashley Frieze    11 年前

    答案二:

    // in the controller where this stuff is needed
    private Map<String, Date> editLocks = new HashMap<>();
    private Object concurrencyLock = new Object();
    
    
    // function for acquiring a lock
    private boolean getLock(String id) {
        synchronized(concurrencyLock) {
            Date lastLock = editLocks.get(id);
            if (lastLock == null || longAgoEnough(lastLock)) {
                // add a new lock to the map with the time of now
                editLocks.put(id, now());
                return true; // a lock was available
            }
        }
        // we get here because there's a valid lock
        return false;
    }
    
    private void releaseLock(String id) {
        synchronized(concurrencyLock) {
            editLocks.remove(id);
        }
    }
    

    您必须在其中添加一点内容才能完成,但它可以在单个服务器上运行得很好。

        2
  •  0
  •   Brian Kates    11 年前

    跟踪“最后一次请求锁定”的时间怎么样?您可以跟踪上一个锁的发出时间,并在请求新锁时更新它。

        3
  •  0
  •   Ashley Frieze    11 年前

    您可以每隔5分钟左右运行一次计划任务,以清除所有孤立的编辑。在方法上使用@Scheduled注释(连同 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html )