(转)实战Memcached缓存系统(5)Memcached的CAS程序实例
1. 非CAS
首先看一个不是CAS的Memcached程序实例。实例的问题原型,见上一篇博文。
程序实例:
- package com.sinosuperman.memcached;
- import java.io.IOException;
- import java.net.InetSocketAddress;
- import net.spy.memcached.MemcachedClient;
- public class Test {
- public static void main(String[] args) throws IOException {
- MemcachedClient cache = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
- cache.set("x", 1800, "Love");
- String obj1 = (String) cache.get("x");
- String obj2 = (String) cache.get("x");
- obj2 = "Michael";
- cache.set("x", 1800, obj2);
- System.out.println("Non-CAS 2:\t" + obj2);
- System.out.println("Non-CAS 1:\t" + obj1);
- }
- }
运行结果:
- 2011-12-18 23:12:39.836 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
- 2011-12-18 23:12:39.843 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@b09e89
- Non-CAS 2: Michael
- Non-CAS 1: Love
可见在多个Client操作时,一定会引起写不一致性的问题。
2. CAS
程序实例:
- package com.sinosuperman.memcached;
- import java.io.IOException;
- import java.net.InetSocketAddress;
- import net.spy.memcached.CASValue;
- import net.spy.memcached.MemcachedClient;
- public class Test {
- @SuppressWarnings("unchecked")
- public static void main(String[] args) throws IOException {
- MemcachedClient cache = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
- cache.set("y", 1800, "Love");
- CASValue casValue1 = cache.gets("y");
- CASValue casValue2 = cache.gets("y");
- cache.cas("y", casValue2.getCas(), casValue2.getValue());
- System.out.println("CAS 2:\t" + casValue2.getCas());
- System.out.println("Value 2:\t" + casValue2.getValue());
- System.out.println("CAS 1:\t" + casValue1.getCas());
- System.out.println("Value 1:\t" + casValue1.getValue());
- }
- }
运行结果:
- 2011-12-18 23:07:14.528 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
- 2011-12-18 23:07:14.541 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@1621e42
- CAS 2: 11
- Value 2: Love
- CAS 1: 11
- Value 1: Love
(转)实战Memcached缓存系统(5)Memcached的CAS程序实例的更多相关文章
- (转)实战Memcached缓存系统(1)Memcached基础及示例程序
1.Cache定义 (1)狭义概念:用于CPU的相对高速处理与主存(Main Memory)的相对低速处理的之间起到协调功能的硬件设备. (2)广义概念:用于速度相差较大的两种硬件之间,起到协调两者数 ...
- 6.memcached缓存系统
1.memcached的安装和参数 memcached缓存系统一般还是部署在linux服务器上,所以这里只介绍linux上memcache的安装 首先切换到root用户,然后apt-get insta ...
- Linux下搭建Memcached缓存系统
首先说下抱歉,博主近期单位经常加班.博客更新有点慢.希望大家理解,草稿箱里存了不少内容,等不忙时候一点点填坑~ 在一般的站点开发学习时候.都会把数据存放在RDBMS(关系型数据库系统(Relation ...
- Memcached 缓存系统简介
memcached官网:http://memcached.org/ What is Memcached? Memcached是一个自由开源的,高性能,高并发,分布式内存对象缓存系统. Memcache ...
- (转)实战Memcached缓存系统(7)Memcached的一些基础FAQ
1. Memcached是什么? Memcached是分布式的内存对象缓存系统. 2. Memcached的基本数据结构是什么? Memcached是基于Key/Value对的HashMap.每一对, ...
- (转)实战Memcached缓存系统(3)Memcached配置参数初解
一.基本参数 在我们第一次安装Memcached时,一般都是用过这个命令: memcached -m 512 -u root -d -l 127.0.0.1 -p 11211 我们先来解释这几个参数的 ...
- django之memcached缓存系统
django其他缓存方法:(https://www.cnblogs.com/jishuweiwang/p/6110809.html) memcached版本 <1.5 1. memcached缓 ...
- (转)实战Memcached缓存系统(8)Memcached异步实时读写问题的解决方案SAC
在使用Memcached时,一般实时读写的场景并不多见.但多是Memcached写入后,在一定时间后才会有读操作.但是如果应用场景,是写入后瞬间即会有读操作呢?似乎没有什么特别之处,我们依然可以这样写 ...
- (转)实战Memcached缓存系统(4)Memcached的CAS协议
1. 什么是CAS协议 很多中文的资料都不会告诉大家CAS的全称是什么,不过一定不要把CAS当作中国科学院(China Academy of Sciences)的缩写.Google.com一下,CAS ...
随机推荐
- HDU 5515 Game of Flying Circus 二分
Game of Flying Circus Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...
- iOS开发——网络编程Swift篇&(六)异步Post方式
异步Post方式 // MARK: - 异步Post方式 func asynchronousPost() { //创建NSURL对象 var url:NSURL! = NSURL(string: &q ...
- android139 360 黑名单 增删改查-数据库操作
BlackNumberOpenHelper.java package com.itheima52.mobilesafe.db.dao; import android.content.Context; ...
- windows快捷操作
命令行启动或关闭VMWare服务: net start VMwareHostdVMAuthdServiceVMUSBArbService"VMware NAT Service"VM ...
- Python_爬虫3
正则表达式 在前面我们已经搞定了怎样获取页面的内容,不过还差一步,这么多杂乱的代码夹杂文字我们怎样把它提取出来整理呢?下面就开始介绍一个十分强大的工具,正则表达式! 1.了解正则表达式 正则表达式是对 ...
- Linux shell 脚本攻略之批量重命名
摘自:<Linux shell 脚本攻略>
- ASP.NET MVC 之 路由配置
主要操作在App_Start 目录下的 RouteConfig.cs 文件. 一.Url构造方式 1.命名参数规范+匿名对象 routes.MapRoute( name: "Default& ...
- Windows Azure 微软公有云体验(一) 网站、SQL数据库、虚拟机
Windows Azure 微软公有云已经登陆中国有一段时间了,现在是处于试用阶段,Windows Azure的使用将会给管理信息系统的开发.运行.维护带来什么样的新体验呢? Windows Azur ...
- webservice发布接口
一:编写接口程序,计算功能类,有加减乘除四个方法 /** * */ package com.hlcui.util; /** * @author Administrator 将此类发布为公共接口 */ ...
- [改善Java代码]注意Class类的特殊性
Java语言是先把Java源文件编译成后缀为class的字节码文件,然后再通过ClassLoader机制把这些类文件加载到内存中,最后生成实例执行的,这是Java处理的基本机制,但加载到内存中的数据是 ...