• Memcached安装与配置


     memcached是danga.com的一个项目。它是一款开源的高性能的分布式内存对象缓存系统。最早是给LiveJournal提供服务的。后来逐渐被越来越多的大型站点所採用。用于在应用中减少对数据库的訪问。提高应用的訪问速度。并减少数据库的负载。

     为了在内存中提供数据的快速查找能力,memcached使用key-value形式存储和訪问数据。在内存中维护一张巨大的HashTable。使得对数据查询的时间复杂度减少到O(1),保证了对数据的高性能訪问。内存的空间总是有限的,当内存没有很多其它的空间来存储新的数据是,memcached就会用LRU算法将近期不常訪问的数据淘汰掉,以腾出空间来存放新的数据。

    memcached存储支持的数据格式也是灵活多样的。通过对象的序列化机制。能够将更高层抽象的对象转换成为二进制数据,存储在缓存服务器中,当前端应用须要时,又能够通过二进制内容反序列化,将数据还原成原对象。


     因为memcached使用了libevent来进行高效的网络连接处理,因此在安装memcached之前,须要先安装libevent。
     全部的安装包能够在这里下载。

    安装libevent
    版本号 libevent-2.0.21-stable.tar.gz(如果放在/root文件夹下)

    [root@zzh ~]# mkdir libevent
    [root@zzh ~]# tar -zvxf libevent-2.0.21-stable.tar.gz
    [root@zzh ~]# cd libevent-2.0.21-stable
    [root@zzh libevent-2.0.21-stable]# ./configure --prefix=/root/libevent
    [root@zzh libevent-2.0.21-stable]# make
    [root@zzh libevent-2.0.21-stable]# make install

    測试libevent是否成功安装(没有必定性。仅仅供參考)

    [root@zzh libevent-2.0.21-stable]# ls -al /root/libevent/lib | grep libevent
    lrwxrwxrwx. 1 root root      21 45 22:05 libevent-2.0.so.5 -> libevent-2.0.so.5.1.9
    -rwxr-xr-x. 1 root root  968722 45 22:05 libevent-2.0.so.5.1.9
    -rw-r--r--. 1 root root 1571586 45 22:05 libevent.a
    lrwxrwxrwx. 1 root root      26 45 22:05 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.9
    -rwxr-xr-x. 1 root root  585265 45 22:05 libevent_core-2.0.so.5.1.9
    -rw-r--r--. 1 root root  978314 45 22:05 libevent_core.a
    -rwxr-xr-x. 1 root root     980 45 22:05 libevent_core.la
    lrwxrwxrwx. 1 root root      26 45 22:05 libevent_core.so -> libevent_core-2.0.so.5.1.9
    lrwxrwxrwx. 1 root root      27 45 22:05 libevent_extra-2.0.so.5 -> libevent_extra-2.0.so.5.1.9
    -rwxr-xr-x. 1 root root  404884 45 22:05 libevent_extra-2.0.so.5.1.9
    -rw-r--r--. 1 root root  593344 45 22:05 libevent_extra.a
    -rwxr-xr-x. 1 root root     987 45 22:05 libevent_extra.la
    lrwxrwxrwx. 1 root root      27 45 22:05 libevent_extra.so -> libevent_extra-2.0.so.5.1.9
    -rwxr-xr-x. 1 root root     945 45 22:05 libevent.la
    lrwxrwxrwx. 1 root root      30 45 22:05 libevent_pthreads-2.0.so.5 -> libevent_pthreads-2.0.so.5.1.9
    -rwxr-xr-x. 1 root root   18462 45 22:05 libevent_pthreads-2.0.so.5.1.9
    -rw-r--r--. 1 root root   18662 45 22:05 libevent_pthreads.a
    -rwxr-xr-x. 1 root root    1008 45 22:05 libevent_pthreads.la
    lrwxrwxrwx. 1 root root      30 45 22:05 libevent_pthreads.so -> libevent_pthreads-2.0.so.5.1.9
    lrwxrwxrwx. 1 root root      21 45 22:05 libevent.so -> libevent-2.0.so.5.1.9

    安装memcached

    [root@zzh ~]# mkdir memcached
    [root@zzh ~]# tar -zvxf memcached-1.4.7.tar.gz
    [root@zzh ~]# cd memcached-1.4.7/
    [root@zzh memcached-1.4.7]# ./configure --prefix=/root/memcached --with-libevent=/root/libevent
    [root@zzh memcached-1.4.7]# make
    [root@zzh memcached-1.4.7]# make install

    启动memcached

    [root@zzh ~]# cd memcached/bin/
    [root@zzh bin]# memcached -d -m 10 -u root -l 10.10.195.112 -p 11211 -c 32 -P /tmp/memcached.pid

    參数描写叙述:
    -d :启动一个守护进程,
    -m:分配给Memcache使用的内存数量。单位是MB,默认是64MB,
    -u :执行Memcache的用户
    -l :监听的服务器IP地址
    -p :设置Memcache监听的端口。默认是11211 注:-p(p为小写)
    -c :设置最大并发连接数,默认是1024
    -P :设置保存Memcache的pid文件 注:-P(P为大写)
    -f :块大小增长因子。默认是1.25
    -n :最小分配空间。key+value+flags默认是48
    -h :显示帮助

    查看memcached进程

    [root@zzh bin]# ps -aux | grep memcached
    Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
    root     13904  0.0  0.0 326708   912 ?        Ssl  22:26   0:00 ./memcached -d -m 10 -u root -l 10.10.195.112 -p 11211 -c 32 -P /tmp/memcached.pid
    root     13912  0.0  0.0 103248   852 pts/2    S+   22:27   0:00 grep memcached

    关闭memcached进程(採用kill pid的命令)

    [root@zzh bin]# kill `cat /tmp/memcached.pid`

    或者

    [root@zzh bin]# kill 13904

    连接memcached

    telnet 127.0.0.1 11211

     memcache有一些经常使用的命令:set, add, replace,append, preappend, cas, get, incr, decr, delete等,具体能够查阅相关文档。


    Javaclient与memcached
     memcached官方提供的Memcached-Java-Client工具包括了对memcached协议的Java封装,使用它能够比較方便地与环城服务端进行通讯。
     案例

    import com.danga.MemCached.MemCachedClient;
    import com.danga.MemCached.SockIOPool;
    import com.schooner.MemCached.MemcachedItem;
    
    public class MemcachedTest
    {
        public static void init()
        {
            String servers[] = {
                    "10.10.195.112:11211"
            };
            SockIOPool pool = SockIOPool.getInstance();
            pool.setServers(servers);
            pool.setFailover(true);
            pool.setInitConn(10);
            pool.setMinConn(5);
            pool.setMaxConn(25);
            pool.setMaintSleep(30);
            pool.setNagle(false);
            pool.setSocketTO(3000);
            pool.setAliveCheck(true);
            pool.setHashingAlg(SockIOPool.CONSISTENT_HASH);
            pool.initialize();
        }
        public static void main(String[] args)
        {
            init();
            MemCachedClient mem = new MemCachedClient();
            mem.add("UserName", "zzzzh");
            mem.prepend("UserName", "hello ");
            mem.append("UserName", "!");
            System.out.println(mem.get("UserName"));
    
            mem.set("key", 1);
            System.out.println(mem.get("key"));
            MemcachedItem item = mem.gets("key");
            mem.cas("key", (Integer)item.getValue()+1,item.getCasUnique());
            System.out.println(mem.get("key"));
        }
    }

    输出结果:

    hello zzzzh!
    1
    2

    參考资料
    1. 《大型分布式站点架构设计与实践》陈康贤著

  • 相关阅读:
    Java设计模式——自定义Spring框架
    《网络协议从入门到底层原理》笔记(二)MAC地址、IP地址、子网划分、超网
    《网络协议从入门到底层原理》笔记(一)基本概念、通信基础
    Java并发编程——共享模型的线程安全问题
    Java 泛型
    Java并发编程——线程基础知识
    Django实战——面试评估系统
    GIT 撤销上一次merge操作
    解决composer install遇到:Your requirements could not be resolved to an installable set of packages
    yii2乐观锁和悲观锁代码示例
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/7351335.html
Copyright © 2020-2023  润新知