1. 개념: 여러 실행의 영향은 한 번의 실행의 영향과 동일합니다.
이 의미에 따르면 최종 의미는 데이터베이스에 미치는 영향은 일회성일 수 있으며 반복적으로 처리할 수 없다는 것입니다. 멱등성을 보장하는 방법은 일반적으로 다음과 같은 방법이 사용됩니다.
1: 데이터베이스는 최종적으로 단 하나의 데이터만 데이터베이스에 삽입되도록 보장할 수 있는 고유 인덱스를 설정합니다.
2: 토큰 메커니즘, 이전에 토큰을 얻습니다. 각 인터페이스 요청을 수행하고 다음에 요청할 때 이 토큰을 요청의 헤더 본문에 추가하고 백그라운드에서 확인합니다. 확인이 통과되면 토큰을 삭제하고 다음 요청에 대해 토큰을 다시 판단합니다
3: 비관적 잠금 또는 낙관적 잠금, 비관적 잠금은 모든 업데이트를 보장할 수 있습니다. 다른 SQL이 데이터를 업데이트할 수 없는 경우(데이터베이스 엔진이 innodb인 경우 선택 조건은 전체 테이블 잠금을 방지하기 위해 고유 인덱스여야 합니다)
4: 먼저 쿼리한 후 판단합니다. 먼저 데이터베이스에 쿼리하여 데이터가 존재하는지 확인합니다. 요청이 통과된 경우 요청이 존재하지 않으면 바로 거부됩니다. 처음으로 들어와 요청이 직접 공개된다는 것을 증명합니다.
Redis는 자동 멱등성의 도식을 실현합니다.
1: 먼저 Redis 서버를 구축합니다.
2: springboot에 redis stater 또는 Spring에 의해 캡슐화된 jedis를 도입합니다. 나중에 사용되는 주요 API는 set 메서드이고 여기서는 springboot의 캡슐화된 redisTemplate을 사용합니다.
/* redis工具类 */ @Component public class RedisService { @Autowired private RedisTemplate redisTemplate; /** * 写入缓存 * @param key * @param value * @return */ public boolean set(final String key,Object value){ boolean result = false; try { ValueOperations<Serializable,Object> operations = redisTemplate.opsForValue(); operations.set(key,value); result = true; }catch (Exception e){ result = false; e.printStackTrace(); } return result; } /** * 写入缓存有效期 * @return */ public boolean setEx(final String key ,Object value,Long expireTime){ boolean result = false; try { ValueOperations<Serializable,Object> operations = redisTemplate.opsForValue(); operations.set(key,value); redisTemplate.expire(key,expireTime, TimeUnit.SECONDS);//有效期 result = true; }catch (Exception e){ result = false; e.printStackTrace(); } return result; } /** * 判断缓存中是否有对应的value * @param key * @return */ public boolean exists(final String key){ return redisTemplate.hasKey(key); } /** * 读取缓存 * @param key * @return */ public Object get(final String key){ Object obj = null; ValueOperations<Serializable,Object> operations= redisTemplate.opsForValue(); obj = operations.get(key); return obj; } /** * 删除对应的value * @param key * @return */ public boolean remvoe(final String key){ if(exists(key)){ Boolean delete = redisTemplate.delete(key); return delete; } return false; } }
package com.yxkj.springboot_redis_interceptor.annotion; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface AutoIdempotent { }
public interface TokenService { /** * 创建token * @return */ String createToken(); /** * 检验token的合法性 * @param request * @return * @throws Exception */ boolean checkToken(HttpServletRequest request) throws Exception; }
위 내용은 springboot가 자동 멱등성 인터페이스를 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!