memcached客户端

Today we will look into Memcached Java client example. Earlier we learned about telnet commands for memcached with sample execution terminal logs. But most of the times we want to connect to the Memcached server through some other programming languages, such as Java and PHP.

今天,我们将研究Memcached Java客户端示例。 之前我们了解了带有示例执行终端日志的用于memcached的telnet命令 。 但是大多数时候,我们希望通过其他一些编程语言(例如Java和PHP)连接到Memcached服务器。

Memcached Java客户端 (Memcached Java Clients)

There are three most widely used memcached Java client API.

有三种使用最广泛的内存缓存Java客户端API。

  1. xmemcachedxmemcached
  2. spymemcachedspymemcached
  3. gwhalin memcached clientgwhalin memcached客户端

Memcached Java示例 (Memcached Java Example)

I have used Greg Whalin Memcached client and found it easy to understand and use. It provides all the basic functionalities with thread pooling. Below is the maven dependency to include this into your project.

我使用了Greg Whalin Memcached客户端,发现它易于理解和使用。 它提供了线程池的所有基本功能。 下面是Maven依赖项,将其包括在您的项目中。

<dependency><groupId>com.whalin</groupId><artifactId>Memcached-Java-Client</artifactId><version>3.0.2</version>
</dependency>

To help you get started quickly, I am providing a sample program to showcase the usage of basic functions that can be performed with the Memcached server.

为了帮助您快速入门,我提供了一个示例程序来展示可以使用Memcached服务器执行的基本功能。

package com.journaldev.memcached.test;import java.util.HashMap;
import com.whalin.MemCached.MemCachedClient;
import com.whalin.MemCached.SockIOPool;public class MemcachedJavaClient {/*** MemcachedJavaClient program to show the usage of different functions* that can be performed on Memcached server with Java Client* @param args*/public static void main(String[] args) {//initialize the SockIOPool that maintains the Memcached Server Connection PoolString[] servers = {"localhost:11111"};SockIOPool pool = SockIOPool.getInstance("Test1");pool.setServers( servers );pool.setFailover( true );pool.setInitConn( 10 );pool.setMinConn( 5 );pool.setMaxConn( 250 );pool.setMaintSleep( 30 );pool.setNagle( false );pool.setSocketTO( 3000 );pool.setAliveCheck( true );pool.initialize();//Get the Memcached Client from SockIOPool named Test1MemCachedClient mcc = new MemCachedClient("Test1");//add some value in cacheSystem.out.println("add status: "+mcc.add("1", "Original"));//Get value from cacheSystem.out.println("Get from Cache: "+mcc.get("1"));System.out.println("add status: "+mcc.add("1", "Modified"));System.out.println("Get from Cache: "+mcc.get("1"));//use set function to add/update value, use replace to update and not addSystem.out.println("set status: "+mcc.set("1","Modified"));System.out.println("Get from Cache after set: "+mcc.get("1"));//use delete function to delete the key from cacheSystem.out.println("remove status: "+mcc.delete("1"));System.out.println("Get from Cache after delete: "+mcc.get("1"));//Use getMulti function to retrieve multiple keys values in one function// Its helpful in reducing network calls to 1mcc.set("2", "2");mcc.set("3", "3");mcc.set("4", "4");mcc.set("5", "5");String [] keys = {"1", "2","3","INVALID","5"};HashMap<String,Object> hm = (HashMap<String, Object>) mcc.getMulti(keys);for(String key : hm.keySet()){System.out.println("KEY: "+key+" VALUE: "+hm.get(key));}}}

The output of the above Memcache java client program is:

上面的Memcache Java客户端程序的输出为:

add status: true
Get from Cache: Original
add status: false
Get from Cache: Original
set status: true
Get from Cache after set: Modified
remove status: true
Get from Cache after delete: null
KEY: 3 VALUE: 3
KEY: 2 VALUE: 2
KEY: 1 VALUE: null
KEY: INVALID VALUE: null
KEY: 5 VALUE: 5

If you want to connect to multiple Memcached servers then you will have to create multiple SockIOPool instances and then use the same name while getting the MemcacheClient instance.

如果要连接到多个Memcached服务器,则必须创建多个SockIOPool实例,然后在获取MemcacheClient实例时使用相同的名称。

翻译自: https://www.journaldev.com/24/memcached-java-client-example

memcached客户端

memcached客户端_Memcached Java客户端示例相关推荐

  1. memcached的java客户端_Memcached Java客户端

    代码示例: import com.danga.MemCached.*; import org.apache.log4j.*; public class TestMemcached { public s ...

  2. memcached 命令_Memcached Telnet命令示例

    memcached 命令 In earlier posts, we saw how to install Memcached server on Mac OS and Unix systems. Af ...

  3. solr java 客户端_Solr JAVA客户端SolrJ的使用

    一.Solrj简介 SolrJ是操作Solr的JAVA客户端,它提供了增加.修改.删除.查询Solr索引的JAVA接口.SolrJ针对 Solr提供了Rest 的HTTP接口进行了封装, SolrJ底 ...

  4. java实现redis客户端_redis java客户端 RedisJava客户端Jredis

    RedisJava客户端Jredis,JRedis 是一个高性能的 Java 客户端,用来连接到Redis分布式哈希键-值数据库.提供同步和异步的连接. 由于jreds的jar包不在公网的maven仓 ...

  5. memcached win64位服务端安装和java客户端实例

    项目开发中需要用到memcached缓存记录下来相关操作方便日后复习,如果有错误或遗漏请留言. memcached服务端安装 下载安装包 下载地址 32位系统 1.4.4版本:http://stati ...

  6. 【Java从0到架构师】Zookeeper 应用 - Java 客户端操作、服务器动态感知、分布式锁业务处理

    分布式基石 Zookeeper 框架全面剖析 Java 客户端操作 Java 客户端 API 服务器的动态感知 服务注册 服务发现 分布式锁业务处理 单机环境(一个虚拟机中) 分布式环境_同名节点 分 ...

  7. 干货 | Elasticsearch Java 客户端演进历史和选型指南

    1.Elasticsearch java 客户端为什么要选型? Elasticsearch 官方提供了很多版本的 Java 客户端,包含但不限于: Transport 客户端 Java REST 客户 ...

  8. Memcached学习笔记 — 第四部分:Memcached Java 客户端-gwhalin(1)-介绍及使用

     介绍 Memcached java client是官方推荐的最早的memcached java客户端.最新版本:java_memcached-release_2.6.1. 官方下载地址:http ...

  9. Memcached的几种Java客户端(待实践)

    其实现在来尝试Memcached的客户端估计会有点过气,因为现在大势基本都在Redis那边. Memcached Client目前有3种: Memcached Client for Java(已经停止 ...

最新文章

  1. Go 知识点(16)— 将枚举值转换为字符串
  2. 寄存器和存储器的区别_寄存器、累加器、暂存器都是什么?它们有什么区别?...
  3. Kingdee v7.0账套修复
  4. 解析Python中的条件语句和循环语句
  5. jdeveloper_适用于JDeveloper 11gR2的Glassfish插件
  6. java的传值调用什么_Java的传值调用
  7. 财经数据提取器上线拉
  8. android studio运行模拟器报错请求超时_GDA关于android脱壳的问题说明
  9. maven 总结整理(二)——download source code
  10. appium在android7.0上无法启动问题
  11. linux平台使用doxygen生成项目文档
  12. airbnb北京民宿运营情况分析
  13. [USACO06DEC]The Fewest Coins G(混合背包)
  14. chrome浏览器安装vue插件
  15. 网易新闻iOS版使用的18个开源组件
  16. 大街上数字标志图片_大街上的开放组织
  17. SAP FICO 第五节 物料分类账ML配置及应用
  18. 时序动作检测《BSN: Boundary Sensitive Network for Temporal Action Proposal Generation》
  19. GESD 离群值检验——理论与 Python 实现
  20. IDM2022最新版极速下载工具

热门文章

  1. 各浏览器的Hack写法【转】
  2. GridView动态添加模版列
  3. [转载] C++ 中的浮点代码优化
  4. vue项目实现详情页后退缓存之前的数据
  5. mysql 权限管理 记录
  6. Hibernate框架学习2
  7. 深入理解 Linux 的 RCU 机制
  8. vb.net如何发送含双引号的字符串。转义双引号
  9. hdu 2553 N皇后问题
  10. javascript div 弹出可拖动窗口