spring + redis to prevent repeated form submission

Introduction:

        spring + redis prevents repeated form submissions.

Implementation:

        1. Complete the integration of spring and reis according to http://see-you-again.iteye.com/admin/blogs/2323435

        2. The core code is as follows:

/**
     * In order to prevent the form from being submitted repeatedly, we need to store a unique identifier in the database, when the form is submitted repeatedly
     * If it is found that the data already exists, then return the insertion failure
     *
     * At the same time, we need to set the timeliness of this unique identifier, but whether the data already exists or not will modify the timeliness value, then
     * The solution is to set the uuid, only when the value is inserted or the value is equal, we are allowed to modify
     * @param singleid unique identifier
     * @return operation result
     */
    public boolean add(final String singleid) {
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
                String uuid = UUID.randomUUID().toString().replaceAll("-","");
                byte[] key  = serializer.serialize(singleid);
                byte[] value = serializer.serialize(uuid);
                boolean res = connection.setNX(key, value);
                if(res || connection.get(key).equals(value))
                  connection.expire(key,122) ;
                return res ;
            }
        });
        return result;
    }

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327058393&siteId=291194637