Here is a way to integrate springboot into ehcache to achieve payment timeout limit. The specific content is as follows:
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.6.11</version></dependency>
Introduce ehcache dependency in pom file
Store the ehcache.xml file under the classpath.
Specified in application.xml:
spring: cache: jcache: config: classpath:ehcache.xml
Class annotation @EnableCaching
Implement core code
/* * Record the timestamp of user payment*/public void pinUser(Object userKey) throws Exception{ Cache cache = manager.getCache(cacheName); Element element = cache.get(userKey); if(element == null){ /*If no user's payment record is found, the record is cached and then continue*/ Element newE = new Element(userKey, new Date().getTime()); cache.put(newE); } else { /*If there is a user's payment record, an exception should be thrown and the user's corresponding information should be prompted*/ long inTime = (Long) element.getObjectValue(); long timeToWait = (getTimeToLive() - (new Date().getTime() - inTime)/1000); //The time to wait is prompted throw new Exception(String.format("It still needs to wait %s seconds",String.valueOf(timeToWait))); }}/* * Delete the timestamp of the user's payment (this method is used to manually remove the user's payment record when the internal payment fails in the system, so as not to affect the user's attempt again) * Normally, this method should not be called, but should be automatically cleared after the cache timeout*/public void unPinUser(Object userKey) { Cache cache = manager.getCache(cacheName); cache.remove(userKey);}/* * Get the cache configuration to convert the time the user still needs to wait, thus giving a more friendly waiting time prompt. */private long getTimeToLive(){ Cache cache = manager.getCache(cacheName); return cache.getCacheConfiguration().getTimeToLiveSeconds();}use
Just call PayToken.getInstance().pinUser(user.getKey()) where the payment interface is called. If an exception is thrown, it means that the payment interval is too small. At the same time, if there are additional data operations, throwing an exception can also trigger a rollback operation.
It is unreasonable to still need the user to wait if the execution fails due to system reasons. Therefore, it is added to remove the user's records PayToken.getInstance().unPinUser(user.getKey()) .
Summarize
The above is the method of springboot integrating ehcache to achieve payment timeout restrictions that the editor introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!