java memcache cas_cas ticket 存储方式-memcache

修改 ticketRegistry.xml :

${memcache.url}

MemCacheTicketRegistry.java

package com.olymtech.cas.ticket.registry;

import java.io.IOException;

import java.net.InetSocketAddress;

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;

import java.util.concurrent.Future;

import net.spy.memcached.MemcachedClient;

import org.jasig.cas.ticket.ServiceTicket;

import org.jasig.cas.ticket.Ticket;

import org.jasig.cas.ticket.TicketGrantingTicket;

import org.springframework.beans.factory.DisposableBean;

import javax.validation.constraints.Min;

import com.olymtech.common.cache.MemCachedFactory;

/**

* Memcache (or Repcache) backed ticket registry.

*

*/

public final class MemCacheTicketRegistry extends AbstractDistributedTicketRegistry implements DisposableBean {

private final MemcachedClient client;

@Min(0)

private final int tgtTimeout;

@Min(0)

private final int stTimeout;

private boolean synchronizeUpdatesToRegistry = false;

/**

* @param hostnames

* @param ticketGrantingTicketTimeOut

* @param serviceTicketTimeOut

*/

public MemCacheTicketRegistry(final String[] hostnames, final int ticketGrantingTicketTimeOut, final int serviceTicketTimeOut) {

System.out.println("use memcached...");

this.tgtTimeout = ticketGrantingTicketTimeOut;

this.stTimeout = serviceTicketTimeOut;

final List addresses = new ArrayList();

for (final String hostname : hostnames) {

String[] hostPort = hostname.split(":");

addresses.add(new InetSocketAddress(hostPort[0], Integer.parseInt(hostPort[1])));

}

try {

this.client = new MemcachedClient(addresses);

} catch (final IOException e) {

throw new IllegalStateException(e);

}

}

/**

* This alternative constructor takes time in milliseconds. It has the

* timeout parameters first because, otherwise it would be almost impossible

* to differentiate the two.

*

* @param ticketGrantingTicketTimeOut

* @param serviceTicketTimeOut

* @param hostnames

*/

public MemCacheTicketRegistry(final long ticketGrantingTicketTimeOut, final long serviceTicketTimeOut, final String[] hostnames) {

this(hostnames, (int) (ticketGrantingTicketTimeOut / 1000), (int) (serviceTicketTimeOut / 1000));

}

private void handleSynchronousRequest(final Future f) {

try {

if (this.synchronizeUpdatesToRegistry) {

f.get();

}

} catch (final Exception e) {

// ignore these.

}

}

protected void updateTicket(final Ticket ticket) {

if (ticket instanceof TicketGrantingTicket) {

handleSynchronousRequest(this.client.replace(ticket.getId(), this.tgtTimeout, ticket));

}

if (ticket instanceof ServiceTicket) {

handleSynchronousRequest(this.client.replace(ticket.getId(), this.stTimeout, ticket));

}

}

public void addTicket(final Ticket ticket) {

if (ticket instanceof TicketGrantingTicket) {

handleSynchronousRequest(this.client.add(ticket.getId(), this.tgtTimeout, ticket));

}

if (ticket instanceof ServiceTicket) {

handleSynchronousRequest(this.client.add(ticket.getId(), this.stTimeout, ticket));

}

}

public boolean deleteTicket(final String ticketId) {

Future f = this.client.delete(ticketId);

try {

return f.get().booleanValue();

} catch (final Exception e) {

log.error(e.getMessage(), e);

return false;

}

}

public Ticket getTicket(final String ticketId) {

final Ticket t = (Ticket) this.client.get(ticketId);

if (t == null) {

return null;

}

return getProxiedTicketInstance(t);

}

public Collection getTickets() {

throw new UnsupportedOperationException("GetTickets not supported.");

}

public void destroy() throws Exception {

this.client.shutdown();

}

public void setSynchronizeUpdatesToRegistry(final boolean b) {

this.synchronizeUpdatesToRegistry = b;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值