XMemcachedClient实例的add方法和set方法区别
2015-06-21 22:36
501 查看
在做memcached存取数据的时候,我们得到XMemcachedClient实例后,向memcached中存数据的[b]方法[/b]是
1 XMemcachedClient.set(String key,int time,Object value);
2 XMemcachedClient.add(String key,int time,Object value);
首先说,这两种[b]方法[/b]一定都能向memcached中存数据,但是二者有什么区别呢?
上网查了很多资料,都没有看到有解释的,于是我决定自己试图测试分析一下:
测试代码:
加载spring的bean.xml文件,里面相关配置,这里不再复述
查看文本打印
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
XMemcachedClient xmc=(XMemcachedClient) context.getBean("memcachedClient");
查看文本打印
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
XMemcachedClient xmc=(XMemcachedClient) context.getBean("memcachedClient");
执行如下代码
查看文本打印
System.out.println(xmc.add("hello", 0, "ppms"));
查看文本打印
System.out.println(xmc.add("hello", 0, "ppms"));
结果为true
很正常
但是这行代码执行两次,第二次就会有错误,原因很简单,XMemcachedClient的add[b]方法[/b]不允许key值相等,即内存中有key为hello的值了,再次执行
add[b]方法[/b],就会失败。而经过测试,第二次执行xmc.set("hello",0,"ppms");就不会出错,原因是set[b]方法[/b]在缓存服务器上,没有对应的key,便新建key-value,
如果有,就替换该key对应的value值。
综上:set和add[b]方法[/b]的不同之处是add[b]方法[/b]不允许key值相同,如果第二次add的key相同,则存储失败,而set[b]方法[/b]允许key相同,如果相同,则替换该key对应的value。
注:每次做测试的时候,第一次add或set后,第二次测试的时候,应该先将memcached中该key所对应的值删除掉,否则该值一直存在缓存服务器中,影响测试结果。
删除[b]方法[/b]很简单:xmc.delete("hello");
1 XMemcachedClient.set(String key,int time,Object value);
2 XMemcachedClient.add(String key,int time,Object value);
首先说,这两种[b]方法[/b]一定都能向memcached中存数据,但是二者有什么区别呢?
上网查了很多资料,都没有看到有解释的,于是我决定自己试图测试分析一下:
测试代码:
加载spring的bean.xml文件,里面相关配置,这里不再复述
查看文本打印
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
XMemcachedClient xmc=(XMemcachedClient) context.getBean("memcachedClient");
查看文本打印
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
XMemcachedClient xmc=(XMemcachedClient) context.getBean("memcachedClient");
执行如下代码
查看文本打印
System.out.println(xmc.add("hello", 0, "ppms"));
查看文本打印
System.out.println(xmc.add("hello", 0, "ppms"));
结果为true
很正常
但是这行代码执行两次,第二次就会有错误,原因很简单,XMemcachedClient的add[b]方法[/b]不允许key值相等,即内存中有key为hello的值了,再次执行
add[b]方法[/b],就会失败。而经过测试,第二次执行xmc.set("hello",0,"ppms");就不会出错,原因是set[b]方法[/b]在缓存服务器上,没有对应的key,便新建key-value,
如果有,就替换该key对应的value值。
综上:set和add[b]方法[/b]的不同之处是add[b]方法[/b]不允许key值相同,如果第二次add的key相同,则存储失败,而set[b]方法[/b]允许key相同,如果相同,则替换该key对应的value。
注:每次做测试的时候,第一次add或set后,第二次测试的时候,应该先将memcached中该key所对应的值删除掉,否则该值一直存在缓存服务器中,影响测试结果。
删除[b]方法[/b]很简单:xmc.delete("hello");
相关文章推荐
- PHP连接Memcache代码
- 安装memcached扩展 验证过了可行
- Memcached入门
- 关于 redis、memcache、mongoDB 的对比
- php使用memcache存储session
- Redis与Memcached的区别
- linux下启动和停止memcached
- nginx+tomcat+memcached集群、负载均衡
- memcached与Redis比较
- php+memcached与Java存取不一致的解决方案
- Memcached学习笔记
- Memcache(MC)系列(八)Memcache ketama一致性哈希算法
- PHP 中安装memcache扩展文件下载对应地址。
- Redis,Memcache,mongoDB的区别
- windows下XAMPP安装php_memcache扩展
- Memcache(MC)系列(七)Memcache stats命令
- Memcache(MC)系列(六)Memcache内存分配策略
- Memcache(MC)系列(五)使用memcache-top监控memcache集群状态
- Memcache(MC)系列(四)度量Memcached性能
- liunx系统安装memcached