1. 非CAS

首先看一个不是CAS的Memcached程序实例。实例的问题原型,见上一篇博文。

程序实例:

  1. package com.sinosuperman.memcached;
  2. import java.io.IOException;
  3. import java.net.InetSocketAddress;
  4. import net.spy.memcached.MemcachedClient;
  5. public class Test {
  6. public static void main(String[] args) throws IOException {
  7. MemcachedClient cache = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
  8. cache.set("x", 1800, "Love");
  9. String obj1 = (String) cache.get("x");
  10. String obj2 = (String) cache.get("x");
  11. obj2 = "Michael";
  12. cache.set("x", 1800, obj2);
  13. System.out.println("Non-CAS 2:\t" + obj2);
  14. System.out.println("Non-CAS 1:\t" + obj1);
  15. }
  16. }

运行结果:

  1. 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
  2. 2011-12-18 23:12:39.843 INFO net.spy.memcached.MemcachedConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@b09e89
  3. Non-CAS 2:  Michael
  4. Non-CAS 1:  Love

可见在多个Client操作时,一定会引起写不一致性的问题。

2. CAS

程序实例:

  1. package com.sinosuperman.memcached;
  2. import java.io.IOException;
  3. import java.net.InetSocketAddress;
  4. import net.spy.memcached.CASValue;
  5. import net.spy.memcached.MemcachedClient;
  6. public class Test {
  7. @SuppressWarnings("unchecked")
  8. public static void main(String[] args) throws IOException {
  9. MemcachedClient cache = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
  10. cache.set("y", 1800, "Love");
  11. CASValue casValue1 = cache.gets("y");
  12. CASValue casValue2 = cache.gets("y");
  13. cache.cas("y", casValue2.getCas(), casValue2.getValue());
  14. System.out.println("CAS 2:\t" + casValue2.getCas());
  15. System.out.println("Value 2:\t" + casValue2.getValue());
  16. System.out.println("CAS 1:\t" + casValue1.getCas());
  17. System.out.println("Value 1:\t" + casValue1.getValue());
  18. }
  19. }

运行结果:

    1. 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
    2. 2011-12-18 23:07:14.541 INFO net.spy.memcached.MemcachedConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@1621e42
    3. CAS 2:  11
    4. Value 2:    Love
    5. CAS 1:  11
    6. Value 1:    Love

(转)实战Memcached缓存系统(5)Memcached的CAS程序实例的更多相关文章

  1. (转)实战Memcached缓存系统(1)Memcached基础及示例程序

    1.Cache定义 (1)狭义概念:用于CPU的相对高速处理与主存(Main Memory)的相对低速处理的之间起到协调功能的硬件设备. (2)广义概念:用于速度相差较大的两种硬件之间,起到协调两者数 ...

  2. 6.memcached缓存系统

    1.memcached的安装和参数 memcached缓存系统一般还是部署在linux服务器上,所以这里只介绍linux上memcache的安装 首先切换到root用户,然后apt-get insta ...

  3. Linux下搭建Memcached缓存系统

    首先说下抱歉,博主近期单位经常加班.博客更新有点慢.希望大家理解,草稿箱里存了不少内容,等不忙时候一点点填坑~ 在一般的站点开发学习时候.都会把数据存放在RDBMS(关系型数据库系统(Relation ...

  4. Memcached 缓存系统简介

    memcached官网:http://memcached.org/ What is Memcached? Memcached是一个自由开源的,高性能,高并发,分布式内存对象缓存系统. Memcache ...

  5. (转)实战Memcached缓存系统(7)Memcached的一些基础FAQ

    1. Memcached是什么? Memcached是分布式的内存对象缓存系统. 2. Memcached的基本数据结构是什么? Memcached是基于Key/Value对的HashMap.每一对, ...

  6. (转)实战Memcached缓存系统(3)Memcached配置参数初解

    一.基本参数 在我们第一次安装Memcached时,一般都是用过这个命令: memcached -m 512 -u root -d -l 127.0.0.1 -p 11211 我们先来解释这几个参数的 ...

  7. django之memcached缓存系统

    django其他缓存方法:(https://www.cnblogs.com/jishuweiwang/p/6110809.html) memcached版本 <1.5 1. memcached缓 ...

  8. (转)实战Memcached缓存系统(8)Memcached异步实时读写问题的解决方案SAC

    在使用Memcached时,一般实时读写的场景并不多见.但多是Memcached写入后,在一定时间后才会有读操作.但是如果应用场景,是写入后瞬间即会有读操作呢?似乎没有什么特别之处,我们依然可以这样写 ...

  9. (转)实战Memcached缓存系统(4)Memcached的CAS协议

    1. 什么是CAS协议 很多中文的资料都不会告诉大家CAS的全称是什么,不过一定不要把CAS当作中国科学院(China Academy of Sciences)的缩写.Google.com一下,CAS ...

随机推荐

  1. 【转】Watir, Selenium & WebDriver

    转自:http://watirmelon.com/2010/04/10/watir-selenium-webdriver/ Please also see my new ‘Watir-WebDrive ...

  2. Codeforces Round #326 (Div. 2) C. Duff and Weight Lifting 水题

    C. Duff and Weight Lifting Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  3. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 分块

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...

  4. iOS开发——图形编程OC篇&粘性动画以及果冻效果

    粘性动画以及果冻效果 在最近做个一个自定义PageControl——KYAnimatedPageControl中,我实现了CALayer的形变动画以及CALayer的弹性动画,效果先过目: 先做个提纲 ...

  5. swift开发:试玩 Apple 站点的 playground

    https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_ ...

  6. redo log write和flush

    http://bbs.chinaunix.net/thread-1753130-1-1.html 在事务提交时innobase会调用ha_innodb.cc 中的innobase_commit,而in ...

  7. android:layout_weight总有你不知道的用法.

    都知道weight是权重的意思. 在布局中起到非常重要的作用. 但是这玩意不能嵌套使用, 而且只能使用在LinearLayout中. 下面说说它的几种用法(以下例子全为横排 注意android:lay ...

  8. mysql查询更新时的锁表机制分析

    为了给高并发情况下的mysql进行更好的优化,有必要了解一下mysql查询更新时的锁表机制. 一.概述 MySQL有三种锁的级别:页级.表级.行级.MyISAM和MEMORY存储引擎采用的是表级锁(t ...

  9. tornado简单的验证码

    1.html代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  10. PHP代码加密 -- php_strip_whitespace函数,去掉源代码所有注释和空格并显示在一行

    <?php function stripCommentAndWhitespace($path = '') { if (empty($path)) { echo '请指定要操作的文件路径'; re ...