map缓存或注册系统

该博客介绍了CAS系统中一个抽象的TicketRegistry类和相关接口,用于管理和验证Ticket。同时,文章详细讲解了一个带过期时间的Map实现,通过定时任务进行清理过期的ProxyGrantingTicket,确保系统的高效运行。
摘要由CSDN通过智能技术生成

tgt的源码:
public final class DefaultTicketRegistry
  extends AbstractTicketRegistry
{
  private final Map<String, Ticket> cache;
  
  public DefaultTicketRegistry()
  {
    this.cache = new ConcurrentHashMap();
  }
  
  public DefaultTicketRegistry(int initialCapacity, float loadFactor, int concurrencyLevel)
  {
    this.cache = new ConcurrentHashMap(initialCapacity, loadFactor, concurrencyLevel);
  }
  
  public void addTicket(Ticket ticket)
  {
    Assert.notNull(ticket, "ticket cannot be null");
    
    this.logger.debug("Added ticket [{}] to registry.", ticket.getId());
    this.cache.put(ticket.getId(), ticket);
  }
  
  public Ticket getTicket(String ticketId)
  {
    if (ticketId == null) {
      return null;
    }
    this.logger.debug("Attempting to retrieve ticket [{}]", ticketId);
    Ticket ticket = (Ticket)this.cache.get(ticketId);
    if (ticket != null) {
      this.logger.debug("Ticket [{}] found in registry.", ticketId);
    }
    return ticket;
  }
  
  public boolean deleteTicket(String ticketId)
  {
    if (ticketId == null) {
      return false;
    }
    this.logger.debug("Removing ticket [{}] from registry", ticketId);
    return this.cache.remove(ticketId) != null;
  }
  
  public Collection<Ticket> getTickets()
  {
    return Collections.unmodifiableCollection(this.cache.values());
  }
  
  public int sessionCount()
  {
    int count = 0;
    for (Ticket t : this.cache.values()) {
      if ((t instanceof TicketGrantingTicket)) {
        count++;
      }
    }
    return count;
  }
  
  public int serviceTicketCount()
  {
    int count = 0;
    for (Ticket t : this.cache.values()) {
      if ((t instanceof ServiceTicket)) {
        count++;
      }
    }
    return count;
  }

}



public abstract class AbstractTicketRegistry
  implements TicketRegistry, TicketRegistryState
{
  protected final Logger logger = LoggerFactory.getLogger(getClass());
  
  public final <T extends Ticket> T getTicket(String ticketId, Class<? extends Ticket> clazz)
  {
    Assert.notNull(clazz, "clazz cannot be null");
    
    Ticket ticket = getTicket(ticketId);
    if (ticket == null) {
      return null;
    }
    if (!clazz.isAssignableFrom(ticket.getClass())) {
      throw new ClassCastException("Ticket [" + ticket.getId() + " is of type " + ticket.getClass() + " when we were expecting " + clazz);
    }
    return (T)ticket;
  }
  
  public int sessionCount()
  {
    this.logger.debug("sessionCount() operation is not implemented by the ticket registry instance {}. Returning unknown as {}", getClass().getName(), Integer.valueOf(-2147483648));
    
    return -2147483648;
  }
  
  public int serviceTicketCount()
  {
    this.logger.debug("serviceTicketCount() operation is not implemented by the ticket registry instance {}. Returning unknown as {}", getClass().getName(), Integer.valueOf(-2147483648));
    
    return -2147483648;
  }

}


public abstract interface TicketRegistry
{
  public abstract void addTicket(Ticket paramTicket);
  
  public abstract <T extends Ticket> T getTicket(String paramString, Class<? extends Ticket> paramClass);
  
  public abstract Ticket getTicket(String paramString);
  
  public abstract boolean deleteTicket(String paramString);
  
  public abstract Collection<Ticket> getTickets();

}


public abstract interface TicketRegistryState
{
  public abstract int sessionCount();
  
  public abstract int serviceTicketCount();
}

带过期时间的map

public class Cas20ProxyReceivingTicketValidationFilter
  extends AbstractTicketValidationFilter
{
  private static final String[] RESERVED_INIT_PARAMS = { "proxyGrantingTicketStorageClass", "proxyReceptorUrl", "acceptAnyProxy", "allowedProxyChains", "casServerUrlPrefix", "proxyCallbackUrl", "renew", "exceptionOnValidationFailure", "redirectAfterValidation", "useSession", "serverName", "service", "artifactParameterName", "serviceParameterName", "encodeServiceUrl", "millisBetweenCleanUps", "hostnameVerifier", "encoding", "config" };
  private static final int DEFAULT_MILLIS_BETWEEN_CLEANUPS = 60000;
  private String proxyReceptorUrl;
  private Timer timer;
  private TimerTask timerTask;
  private int millisBetweenCleanUps;
  private ProxyGrantingTicketStorage proxyGrantingTicketStorage = new ProxyGrantingTicketStorageImpl();

 

 public void init()
  {
    super.init();
    CommonUtils.assertNotNull(this.proxyGrantingTicketStorage, "proxyGrantingTicketStorage cannot be null.");
    if (this.timer == null) {
      this.timer = new Timer(true);
    }
    if (this.timerTask == null) {
      this.timerTask = new CleanUpTimerTask(this.proxyGrantingTicketStorage);
    }
    this.timer.schedule(this.timerTask, this.millisBetweenCleanUps, this.millisBetweenCleanUps);
  }


}




import java.util.TimerTask;


public final class CleanUpTimerTask
  extends TimerTask
{
  private final ProxyGrantingTicketStorage proxyGrantingTicketStorage;
  
  public CleanUpTimerTask(ProxyGrantingTicketStorage proxyGrantingTicketStorage)
  {
    this.proxyGrantingTicketStorage = proxyGrantingTicketStorage;
  }
  
  public void run()
  {
    this.proxyGrantingTicketStorage.cleanUp();
  }

}





public final class ProxyGrantingTicketStorageImpl
  implements ProxyGrantingTicketStorage
{
  private final Log log = LogFactory.getLog(getClass());
  private static final long DEFAULT_TIMEOUT = 60000L;
  private final ConcurrentMap<String, ProxyGrantingTicketHolder> cache = new ConcurrentHashMap();
  private long timeout;
  
  public ProxyGrantingTicketStorageImpl()
  {
    this(60000L);
  }
  
  public ProxyGrantingTicketStorageImpl(long timeout)
  {
    this.timeout = timeout;
  }
  
  public String retrieve(String proxyGrantingTicketIou)
  {
    ProxyGrantingTicketHolder holder = (ProxyGrantingTicketHolder)this.cache.get(proxyGrantingTicketIou);
    if (holder == null)
    {
      this.log.info("No Proxy Ticket found for [" + proxyGrantingTicketIou + "].");
      return null;
    }
    this.cache.remove(proxyGrantingTicketIou);
    if (this.log.isDebugEnabled()) {
      this.log.debug("Returned ProxyGrantingTicket of [" + holder.getProxyGrantingTicket() + "]");
    }
    return holder.getProxyGrantingTicket();
  }
  
  public void save(String proxyGrantingTicketIou, String proxyGrantingTicket)
  {
    ProxyGrantingTicketHolder holder = new ProxyGrantingTicketHolder(proxyGrantingTicket);
    if (this.log.isDebugEnabled()) {
      this.log.debug("Saving ProxyGrantingTicketIOU and ProxyGrantingTicket combo: [" + proxyGrantingTicketIou + ", " + proxyGrantingTicket + "]");
    }
    this.cache.put(proxyGrantingTicketIou, holder);
  }
  
  public void cleanUp()
  {
    for (Map.Entry<String, ProxyGrantingTicketHolder> holder : this.cache.entrySet()) {
      if (((ProxyGrantingTicketHolder)holder.getValue()).isExpired(this.timeout)) {
        this.cache.remove(holder.getKey());
      }
    }
  }
  
  private static final class ProxyGrantingTicketHolder
  {
    private final String proxyGrantingTicket;
    private final long timeInserted;
    
    protected ProxyGrantingTicketHolder(String proxyGrantingTicket)
    {
      this.proxyGrantingTicket = proxyGrantingTicket;
      this.timeInserted = System.currentTimeMillis();
    }
    
    public String getProxyGrantingTicket()
    {
      return this.proxyGrantingTicket;
    }
    
    final boolean isExpired(long timeout)
    {
      return System.currentTimeMillis() - this.timeInserted > timeout;
    }
  }

}


每一次操作都要更新状态:

 protected final void updateState()
  {
    this.previousLastTimeUsed = this.lastTimeUsed;
    this.lastTimeUsed = System.currentTimeMillis();
    this.countOfUses += 1;
  }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值