本文整理汇总了Java中net.rubyeye.xmemcached.MemcachedClient类的典型用法代码示例。如果您正苦于以下问题:Java MemcachedClient类的具体用法?Java MemcachedClient怎么用?Java MemcachedClient使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MemcachedClient类属于net.rubyeye.xmemcached包,在下文中一共展示了MemcachedClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doGet
import net.rubyeye.xmemcached.MemcachedClient; //导入依赖的package包/类
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
ServletException {
String addr =
System.getenv().containsKey("GAE_MEMCACHE_HOST")
? System.getenv("GAE_MEMCACHE_HOST") : "localhost";
String port =
System.getenv().containsKey("GAE_MEMCACHE_HOST")
? System.getenv("GAE_MEMCACHE_PORT") : "11211";
String key = "count";
MemcachedClientBuilder builder = new XMemcachedClientBuilder(
AddrUtil.getAddresses(addr + ":" + port));
MemcachedClient client = builder.build();
long count = 0L;
try {
count = client.incr(key, 1L, 0L);
} catch (TimeoutException | InterruptedException | MemcachedException e) {
throw new ServletException("Memcache error", e);
}
resp.setContentType("text/plain");
resp.getWriter().print("Value is " + count + "\n");
}
示例2: MemcachedService
import net.rubyeye.xmemcached.MemcachedClient; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public MemcachedService(Map<String, Object> srvCfg) {
super.setResponseContentType(srvCfg);
Map<String, String> statement = (Map<String, String>) srvCfg.get("statement");
if (statement != null) {
this.operate = statement.get("operate");
String sourceName = (String) statement.get("source");
if (StringUtils.isNotBlank(sourceName)) {
DataSource ds = DataSourcePool.getDataSource(sourceName);
if (ds != null) {
Object session = ds.getSession();
if (session instanceof MemcachedClient) {
this.client = (MemcachedClient) session;
}
} else {
throw new NullPointerException(String.format("Data source %s is not found", sourceName));
}
} else {
throw new NullPointerException("Service source name is null");
}
} else {
throw new NullPointerException("Service statement is null");
}
}
示例3: create
import net.rubyeye.xmemcached.MemcachedClient; //导入依赖的package包/类
@Override
public CacheClient create(final List<InetSocketAddress> addrs, final CacheConfiguration conf) throws IOException {
MemcacheClientWrapper clientWrapper = (MemcacheClientWrapper)super.create(addrs, conf);
if (resolver==null) {
return clientWrapper;
}
final MemcachedClient memcachedClient = (MemcachedClient) clientWrapper.getNativeClient();
Object proxyInstance = Proxy.newProxyInstance( getClass().getClassLoader(), new Class[]{ MemcachedClient.class }, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if(resolver.get().isPresent()) {
String routingKey = resolver.get().get();
memcachedClient.beginWithNamespace(routingKey);
try {
return method.invoke(memcachedClient, args);
} finally {
memcachedClient.endWithNamespace();
}
} else {
throw new IllegalStateException("Unresolved routing key");
}
}
});
return new MemcacheClientWrapper((MemcachedClient) proxyInstance) ;
}
示例4: create
import net.rubyeye.xmemcached.MemcachedClient; //导入依赖的package包/类
/**
* Create a memcached client with params.
*
* @param hosts whitespace separated host or IP addresses and port numbers
* of the form "host:port host2:port hostN:portN"
* @param protocol opcional, BINARY or TEXT
* @param user opcional, user name o null
* @param pass opcional, password o null
* @param authMechanisms opcional, CRAM-MD5 and/or PLAIN
* @return memcached client
*/
public static MemcachedClient create(
String hosts,
CommandFactory protocol,
String user,
String pass,
String[] authMechanisms) {
MemcachedClient client = null;
try {
MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(hosts));
builder.setCommandFactory(protocol);
if (isNotNullOrEmpty(user)) {
builder.addAuthInfo(
AddrUtil.getAddresses(hosts).get(0),
new AuthInfo(
new PlainCallbackHandler(user, pass),
authMechanisms));
}
client = builder.build();
} catch (IOException ex) {
log.error("An error occurred when creating the MemcachedClient.", ex);
throw new PippoRuntimeException(ex);
}
return client;
}
示例5: main
import net.rubyeye.xmemcached.MemcachedClient; //导入依赖的package包/类
public static void main(String[] args)
throws IOException, TimeoutException, InterruptedException, MemcachedException {
MemcachedClientBuilder builder = new XMemcachedClientBuilder("localhost:11211");
MemcachedClient client = builder.build();
try {
ICacheEntrySerializer ces = DefaultCacheEntrySerializer.instance;
TestValue.Value v1 = new TestValue.Value();
CacheEntry ce = new CacheEntry("key", v1);
byte[] dataSet = ces.serialize(ce);
client.set("key", 0, dataSet);
System.out.println(dataSet.length);
byte[] dataGet = client.get("key");
System.out.println(dataGet.length);
} finally {
client.shutdown();
}
}
示例6: simpleClient
import net.rubyeye.xmemcached.MemcachedClient; //导入依赖的package包/类
private static MemcachedClient simpleClient() {
// AuthDescriptor ad = AuthDescriptor.typical("coca", "coca");
MemcachedClient mc = null;
try {
// mc = new MemcachedClient(new
// ConnectionFactoryBuilder().setProtocol(ConnectionFactoryBuilder.Protocol.BINARY).build(),
// AddrUtil.getAddresses("localhost:11211"));
MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("localhost:11211"));
mc = builder.build();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
return mc;
}
示例7: setApplicationContext
import net.rubyeye.xmemcached.MemcachedClient; //导入依赖的package包/类
@SuppressWarnings("static-access")
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
this.client = getBean("memcachedClient", MemcachedClient.class);
}
示例8: MemCacheMapCache
import net.rubyeye.xmemcached.MemcachedClient; //导入依赖的package包/类
public MemCacheMapCache(String name, MemcachedClient memcachedClient)
throws CacheException {
if (StringUtils.isBlank(name))
throw new CacheException("缓存key不允许为空.");
if (memcachedClient == null) {
throw new CacheException("memcachedClient不允许为空.");
}
this.memcachedClient = memcachedClient;
this.name = name;
}
示例9: SyncLockMapCache
import net.rubyeye.xmemcached.MemcachedClient; //导入依赖的package包/类
public SyncLockMapCache(String name, MemcachedClient memcachedClient,
LocalZookeeperLock lock) throws CacheException {
if (StringUtils.isBlank(name))
throw new CacheException("缓存key不允许为空.");
this.name = name;
this.memcachedClient = memcachedClient;
this.lock = lock;
}
示例10: getMemcachedClient
import net.rubyeye.xmemcached.MemcachedClient; //导入依赖的package包/类
/**
* 获取Memcached连接
* @return Memcached连接
*/
private MemcachedClient getMemcachedClient(){
if(memcachedClient == null || memcachedClient.isShutdown()) {
try {
memcachedClient = memcachedClientBuilder.build();
} catch (IOException e) {
e.printStackTrace();
}
}
return memcachedClient;
}
示例11: containsKey
import net.rubyeye.xmemcached.MemcachedClient; //导入依赖的package包/类
@Override
public boolean containsKey(Object key) {
MemcachedClient memcachedClient = getMemcachedClient();
try{
return memcachedClient.get((String) key)!=null;
}catch (Exception e){
Logger.error(e);
return false;
}
}
示例12: get
import net.rubyeye.xmemcached.MemcachedClient; //导入依赖的package包/类
@Override
public String get(Object key) {
MemcachedClient memcachedClient = getMemcachedClient();
try{
return memcachedClient.get((String) key);
}catch (Exception e){
Logger.error(e);
return null;
}
}
示例13: put
import net.rubyeye.xmemcached.MemcachedClient; //导入依赖的package包/类
@Override
public String put(String key, String value) {
MemcachedClient memcachedClient = getMemcachedClient();
try{
if(memcachedClient.set(key, 0, value)) {
return value;
}else{
return null;
}
}catch (Exception e){
Logger.error(e);
return null;
}
}
示例14: putWithNoReply
import net.rubyeye.xmemcached.MemcachedClient; //导入依赖的package包/类
/**
* 像 memcached 中放置对象数据
* 不管数据存在不存在都会将目前设置的数据存储的 memcached,但不等待返回确认
* @param key key 名称
* @param value 数据
* @param expire 超时时间
* @return true: 成功, false:失败
*/
public Boolean putWithNoReply(String key, Object value, int expire) {
MemcachedClient memcachedClient = null;
try {
memcachedClient = getMemcachedClient();
memcachedClient.setWithNoReply(key, expire, value);
} catch (Exception e) {
Logger.error(e);
return false;
}
return true;
}
示例15: remove
import net.rubyeye.xmemcached.MemcachedClient; //导入依赖的package包/类
@Override
public String remove(Object key) {
MemcachedClient memcachedClient = getMemcachedClient();
try{
String value = memcachedClient.get(key.toString());
memcachedClient.delete(key.toString());
return value;
}catch (Exception e){
Logger.error(e);
return null;
}
}