Class BootCache_RedisImple

java.lang.Object
org.summerboot.jexpress.integration.cache.BootCache_RedisImple
All Implemented Interfaces:
Comparable, HealthInspector, AuthTokenCache, BootCache

public class BootCache_RedisImple extends Object implements AuthTokenCache, BootCache
1. download redis-cell from https://github.com/brandur/redis-cell/releases 2. unzip 3. redis-cli>module load /path/to/libredis_cell.so
Author:
Changski Tie Zheng Zhang 张铁铮, 魏泽北, 杜旺财, 杜富贵
  • Field Details

  • Constructor Details

    • BootCache_RedisImple

      public BootCache_RedisImple()
  • Method Details

    • execute

      protected void execute(JedisCall caller)
    • execute

      protected void execute(boolean retry, JedisCall caller)
    • executeEx

      protected void executeEx(JedisCall caller, int currentRetry, int maxRetry)
    • onRedisDown

      protected void onRedisDown(Throwable ex)
    • onNoticeRedisDown

      protected void onNoticeRedisDown(String info, Throwable ex)
    • onNoticeAutoFailover

      protected void onNoticeAutoFailover(String info, String newNode)
    • tryLock

      public boolean tryLock(String lockName, String unlockPassword, long ttlToExpireIncaseUnableToUnlock, TimeUnit timeUnit)
      this is a Distributed non-blocking version of lock() method; it attempts to acquire the lock immediately, return true if locking succeeds
      Specified by:
      tryLock in interface BootCache
      Parameters:
      lockName - the name of the tryLock
      unlockPassword - unlockPassword is to be used for unlock. To protect a tryLock from being unlocked by anyone, a tryLock cannot be released when unlockPassword not match
      ttlToExpireIncaseUnableToUnlock - expire time of tryLock in case unable to unlock (e.g. exception/error before executing unlock)
      timeUnit -
      Returns:
      the result of get tryLock
    • unlock

      public boolean unlock(String lockName, String unlockPassword)
      unlocks the Distributed Lock instance
      Specified by:
      unlock in interface BootCache
      Parameters:
      lockName - the name of the tryLock
      unlockPassword - to ensure only the owner is able to unlock, success only when this value equals the unlockPassword specified by tryLock
      Returns:
      the result of get release
    • blacklist

      public void blacklist(String key, String value, long ttlMilliseconds)
      Specified by:
      blacklist in interface AuthTokenCache
    • isBlacklist

      public boolean isBlacklist(String key)
      Specified by:
      isBlacklist in interface AuthTokenCache
    • flashsaleInventoryInit

      public boolean flashsaleInventoryInit(String itemId, long totalAmount, long limit)
      Description copied from interface: BootCache
      flash sale - init inventory
      Specified by:
      flashsaleInventoryInit in interface BootCache
      Parameters:
      itemId -
      totalAmount - total inventory
      limit - max orderAmount per order
      Returns:
      true if success
    • flashsaleInventoryReport

      public FlashSale flashsaleInventoryReport(String itemId)
      Description copied from interface: BootCache
      flash sale - inventory report
      Specified by:
      flashsaleInventoryReport in interface BootCache
      Parameters:
      itemId -
      Returns:
    • flashsaleEnable

      public void flashsaleEnable(String itemId, boolean isEnabled)
      Description copied from interface: BootCache
      flash sale - enable
      Specified by:
      flashsaleEnable in interface BootCache
      Parameters:
      itemId -
      isEnabled - sale enabled if true, else disabled
    • flashsaleAcquireQuota

      public long flashsaleAcquireQuota(String itemId, long requestAmount)
      Description copied from interface: BootCache
      flash sale - order competition before reducing inventory
      Specified by:
      flashsaleAcquireQuota in interface BootCache
      Parameters:
      itemId -
      requestAmount -
      Returns:
      confirmed order amount
    • flashsaleRevokeQuota

      public long flashsaleRevokeQuota(String itemId, long requestAmount)
      Description copied from interface: BootCache
      flash sale - revoke an order (undo order competition, normally happens when failed to pay within N minutes)
      Specified by:
      flashsaleRevokeQuota in interface BootCache
      Parameters:
      itemId -
      requestAmount -
      Returns:
      the updated inventory booked amount
    • rateLimiterGetWaitTime

      public long rateLimiterGetWaitTime(String key, int initBurst, int maxBurstPerPeriod, int period, int requestQuota)
      Require Redis 4.0+ and Redis-Cell
      Parameters:
      key -
      initBurst -
      maxBurstPerPeriod -
      period - seconds
      requestQuota -
      Returns:
      The number of seconds until the user should retry, and always -1 if the action was allowed
    • rateLimiterGetSlidingWindowRate

      @Deprecated public long rateLimiterGetSlidingWindowRate(String key, int periodSecond)
      Deprecated.
      Only good for: low rate, check if a user input wrong password more than X times within N minutes.

      Not good for: high rate

      1. Implemented via ZSet, will keep a record/access, not memory friendly for high volumn access.

      2. Not atomic, not accurite with high concurrent access

      3. result is based on current burst ratet, it won't return true if the burst rate keeps high, no matter how long it lasts

      Parameters:
      key -
      periodSecond -
      Returns:
      the burst rate of the current period window