(转)实战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 ...
随机推荐
- Cocos2d-x中由sprite来驱动Box2D的body运动(用来制作平台游戏中多变的机关)
好久都没写文章了,就来一篇吧.这种方法是在制作<胖鸟大冒险>时用到的.<胖鸟大冒险>中使用Box2D来进行物理模拟和碰撞检測,因此对每一个机关须要创建一个b2body.然后&l ...
- 【WebForm】Repeater 序列号 在翻页情况下自增
asp.net Repeater控件分页时,序号列翻页重新从1开始计数问题的解决思路及方法: 一般情况下,使用 <%# Container.ItemIndex + 1% > 给序号列来自增 ...
- 文献阅读笔记——group sparsity and geometry constrained dictionary
周五实验室有同学报告了ICCV2013的一篇论文group sparsity and geometry constrained dictionary learning for action recog ...
- MYSQL源码 与 DBUG
一.前言 在规模稍微大点的项目中,为了方便快速找到bug的所在,我们往往需要在代码中加入一些调试用的代码,比如加入一些printf,打印出一些重点的信息:加入assert,进行断言判断.这些比较随意的 ...
- windows下的python扩展包下载地址
比如lxml什么的 Unofficial Windows Binaries for Python Extension Packages pip install xxx.whl
- LeetCode: Binary Tree Traversal
LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...
- 详细的OS X Yosemite 10.10懒人版安装教程
永远记住一句话:难,是因为不会.先是要放宽心态,才更利于解决安装过程中这样那样的问题.多尝试多动脑,不要有过份的依赖.很多问题到解决以后,才发现是如此的简单,我装黑苹果是拿来使用的,所以我的目的是装好 ...
- mysql权限及用户
一:Flush table tables_name MySQL的FLUSH句法(清除或者重新加载内部缓存) FLUSH flush_option [,flush_option],如果你想要清除一些My ...
- CentOS5.6 安装RabbitMQ
步骤参考官方地址:http://www.rabbitmq.com/install-rpm.html我们这个版本按照官方的不能正确安装. 1.安装erlang(官网地址http://www.erlang ...
- __asm__ __volatile__("": : :"memory");
参考:http://stackoverflow.com/questions/14950614/working-of-asm-volatile-memory asmvolatile("&quo ...