码迷,mamicode.com
首页 > 系统相关 > 详细

Memcached总结四:用ava程序连接memcached进行操作

时间:2015-10-18 22:57:02      阅读:365      评论:0      收藏:0      [点我收藏+]

标签:

1. Memcached的Java环境设置

需要下载spymemcached-2.10.3.jar,并把这个jar放到java程序的classpath中才能使用memcached。

在下面的程序,假设memcached服务器的主机IP是192.168.1.111,并在端口11211上运行。

1. Memcached添加数据(set方法)

技术分享
/**
     * set方法
     */
    public static void set() {
        try {
            MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
                    "192.168.1.111", 11111));
            // not set data into memcached server
            System.out.println("set status:"
                    + mcc.set("hello", 900, "helloworld"));
            // Get value from cache
            System.out.println("Get from Cache:" + mcc.get("hello"));
            mcc.shutdown();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
View Code

2. Memcached添加数据(add方法)

技术分享
/**
     * add方法
     */
    public static void add() {
        try {
            MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
                    "192.168.1.111", 11211));
            mcc.add("key", 900, "memcached");
            System.out.println(mcc.get("key"));
            mcc.shutdown();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
View Code

3. Memcached替换/Replace数据(replace方法)

技术分享
/**
     * replace方法
     */
    public static void replace() {
        try {
            MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
                    "192.168.1.111", 11211));
            // 修改存在的键
            System.out.println(mcc.get("key"));
            mcc.replace("key", 900, "mongodb");
            System.out.println(mcc.get("key"));// mongodb
            // 修改不存在的键
            mcc.replace("no", 900, "noexit");
            System.out.println(mcc.get("no"));// null
            mcc.shutdown();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
View Code

4. Memcached追加/append方法(append方法)

技术分享
/**
     * append
     */
    public static void append() {
        try {
            MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
                    "192.168.1.111", 11211));
            System.out.println(mcc.get("key"));
            mcc.append("key", "nosql");
            System.out.println(mcc.get("key"));
            mcc.shutdown();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
View Code

5. Memcached预先添(prepend方法)

技术分享
/**
     * prepend
     */
    public static void prepend() {
        try {
            MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
                    "192.168.1.111", 11211));
            System.out.println(mcc.get("key"));
            mcc.prepend("key", "redis");
            System.out.println(mcc.get("key"));
            mcc.shutdown();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
View Code

6. Memcached cas命令(cas方法)

cas即checked and set的意思,只有当最后一个参数和gets所获取的参数匹配时才能存储,否则返回“EXISTS”。

要运行memcached的cas命令,需要从gets命令得到memcached令牌。

技术分享
/**
     * cas方法
     */
    public static void cas() {
        try {
            MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
                    "192.168.1.111", 11211));
            System.out.println(mcc.get("key"));
            long casToken = mcc.gets("key").getCas();
            System.out.println(casToken);
            mcc.cas("key", casToken, 900, "mongodb");
            System.out.println(mcc.get("key"));
            mcc.shutdown();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
View Code

7. Memcached获取/get数据(get方法)

技术分享
/**
     * get方法
     */
    public static void get() {
        try {
            MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
                    "192.168.1.111", 11211));
            // not set data into memcached server
            System.out.println("set status:"
                    + mcc.set("hello", 900, "helloworld"));
            // Get value from cache
            System.out.println("Get from Cache:" + mcc.get("hello"));
            mcc.shutdown();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
View Code

8. Memcached gets命令(gets方法)

技术分享
/**
     * gets方法
     */
    public static void gets() {
        try {
            MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
                    "192.168.1.111", 11211));
            long casToken = mcc.gets("key").getCas();
            System.out.println(casToken);
            mcc.shutdown();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
View Code

9. Memcached删除/Delete数据(delete方法)

技术分享
/**
     * delete方法
     */
    public static void delete() {
        try {
            MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
                    "192.168.1.111", 11211));
            System.out.println(mcc.get("key"));
            mcc.delete("key");
            System.out.println(mcc.get("key"));
            mcc.shutdown();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
View Code

10. Memcached递增递减数据(incr方法和decr方法)

Memcached的incr 和 decr命令用于增加现有键的数值递减。如果键未找到或如果关键的不是数字,则返回NOT_FOUND。那么CLIENT_ERROR不能增加或返回递减非数值错误。

技术分享
/**
     * incr方法和decr方法
     */
    public static void incr_decr() {
        try {
            MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
                    "192.168.1.111", 11211));
            System.out.println(mcc.get("num"));
            mcc.incr("num", 5);
            System.out.println(mcc.get("num"));
            System.out.println("------------------>");
            mcc.decr("num", 10);
            System.out.println(mcc.get("num"));
            mcc.shutdown();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
View Code

11. Memcached Stats命令(stats命令)

技术分享
    /**
     * stats方法
     */
    public static void stats() {
        try {
            MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
                    "192.168.1.111", 11211));
            System.out.println(mcc.getStats());
            mcc.shutdown();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
View Code

12. Memcached清除数据(flush方法)

技术分享
/**
     * flush_all方法
     */
    public static void flush_all() {
        try {
            MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
                    "192.168.1.111", 11211));
            System.out.println(mcc.flush().isDone());
            mcc.shutdown();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
View Code

 

Memcached总结四:用ava程序连接memcached进行操作

标签:

原文地址:http://www.cnblogs.com/longshiyVip/p/4890525.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!