当你听到memcache与memcached时把它当做是一个东东就好了,尽管它们存在区别,但是这并不影响你对它们的运用及理解。

“Memcache”它是一个自由和开放源代码、高性能、分配的内存对象缓存系统,即该系统名称为“Memcache”;

“Memcached”它是该系统的主程序文件,以守护程序方式运行于一个或多个服务器中(分布式),随时接受客户端的连接操作,使用共享内存存取数据;

“Memcachedb”它是新浪2007年的项目,在Memcached的基础上开发出来了,它与Memcache不同的是它提供了数据持久化存储

源代码地址:http://code.google.com/p/memcached/downloads/list

首先,我们需要下载一个memcached安装程序,memcached版本很多,开源的东西我们一定要找一个持续更新的版本,很简单,有团队在维护升级。

我选择的版本是:beitmemcached,项目地址:http://code.google.com/p/beitmemcached/  注:此链结为windows下memcached文程序安装文件及示例。

http://code.google.com/p/beitmemcached/downloads/list

图中的两个文件分别为:上面的是客户端调用示例项目文件、下面的文件是Memcached主程序安装文件

然后,将memcached主程序文件安装到服务器上。

Windows下安装:

1.将上图中Memcached 1.2.5.zip解压缩到 D:\program files\memcached目录下(此目录自行定义)。

2.Ctrl+R,输入cmd,打开命令行窗口,转到D:\program files\memcached目录下。

3.memcached.exe -d install

4.memcached.exe -d start

如果你要卸载,执行下面的命令:

1.memcached.exe -d stop

2.memcached.exe -d uninstall

Linux(CentOS 5.x)下安装:

1. yum install gcc

2. cd /tmp

3. wget http://www.monkey.org/~provos/libevent-2.0.4-alpha.tar.gz   注:memcached 用到了 libevent 这个库用于 Socket 的处理,所以 还需要安装 libevent

4. tar zxvf libevent-2.0.4-alpha.tar.gz

5. cd libevent-2.0.4-alpha

6. ./configure -prefix=/usr/local/libevent

7. make

8. make install

9. cd ~

10. cd /tmp

11. http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz

12. tar zxvf memcached-1.4.5.tar.gz

13. cd memcached-1.4.5

14. ./configure -prefix=/usr/local/memcached --with-libevent=/usr/local/libevent    注:安装memcached时需要指定libevent的安装位置

15. make

16. make install

17. export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/libevent/lib   注:将libevent的lib目录加入LD_LIBRARY_PATH里

18. vi /etc/sysconfig/iptables

19. 将下面这行加入进去

-A RH-Firewall-l-INPUT -p tcp -m tcp --dport 11211 -j ACCEPT  注:将memcached加入到防火墙允许访问规则中

20. service iptables restart  注:防火墙重启

21. /usr/local/memcached/bin/memcached -d   注:启动memcached

memcached启动参数描述:

-d :启动一个守护进程,

-m:分配给Memcache使用的内存数量,单位是MB,默认是64MB,

-u :运行Memcache的用户

-l  :监听的服务器IP地址

-p :设置Memcache监听的端口,默认是11211    注:-p(p为小写)

-c :设置最大并发连接数,默认是1024

-P :设置保存Memcache的pid文件   注:-P(P为大写)

如果要结束Memcache进程,执行:kill cat pid文件路径

无论是在windows下还是在linux下安装都非常简单,使用起来也很简单。

如何往memcached中插入数据?如何来读取数据?示例代码如下:

using System;
using System.Collections.Generic;

namespace BeIT.MemCached {
    class Example {
        public static void Main(string[] args) {
            //---------------------
            // Setting up a client.
            //---------------------
            Console.Out.WriteLine("Setting up Memcached Client.");
            MemcachedClient.Setup("MyCache", new string[] { "localhost" });

//It is possible to have several clients with different configurations:
            //If it is impossible to resolve the hosts, this method will throw an exception.
            try {
                MemcachedClient.Setup("MyOtherCache", new string[]{ "server1.example.com:12345", "server2.example.com:12345"});
            } catch (Exception e) {
                Console.WriteLine(e.Message);
            }

//Get the instance we just set up so we can use it. You can either store this reference yourself in
            //some field, or fetch it every time you need it, it doesn't really matter.
            MemcachedClient cache = MemcachedClient.GetInstance("MyCache");

//It is also possible to set up clients in the standard config file. Check the section "beitmemcached"
            //in the App.config file in this project and you will see that a client called "MyConfigFileCache" is defined.
            MemcachedClient configFileCache = MemcachedClient.GetInstance("MyConfigFileCache");

//Change client settings to values other than the default like this:
            cache.SendReceiveTimeout = 5000;
            cache.ConnectTimeout = 5000;
            cache.MinPoolSize = 1;
            cache.MaxPoolSize = 5;

//----------------
            // Using a client.
            //----------------

//Set some items
            Console.Out.WriteLine("Storing some items.");
            cache.Set("mystring", "The quick brown fox jumped over the lazy dog.");
            cache.Set("myarray", new string[]{"This is the first string.", "This is the second string."});
            cache.Set("myinteger", 4711);
            cache.Set("mydate", new DateTime(2008, 02, 23));
            //Use custom hash
            cache.Set("secondstring", "Flygande b鋍kasiner s鰇a hwila p?mjuka tufvor", 4711);

//Get a string
            string str = cache.Get("mystring") as string;
            if (str != null) {
                Console.Out.WriteLine("Fetched item with key: mystring, value: " + str);
            }

//Get an object
            string[] array = cache.Get("myarray") as string[];
            if (array != null) {
                Console.Out.WriteLine("Fetched items with key: myarray, value 1: " + array[0] + ", value 2: " + array[1]);
            }

//Get several values at once
            object[] result = cache.Get(new string[]{"myinteger", "mydate"});
            if (result[0] != null && result[0] is int) {
                Console.Out.WriteLine("Fetched item with key: myinteger, value: " + (int)result[0]);
            }
            if (result[1] != null && result[1] is DateTime) {
                Console.Out.WriteLine("Fetched item with key: mydate, value: " + (DateTime)result[1]);
            }

str = cache.Get("secondstring", 4711) as string;
            if (str != null) {
                Console.Out.WriteLine("Fetched item with key and custom hash: secondstring, value: " + str);
            }

//Set a counter
            Console.Out.WriteLine("Setting an item for incrementing and decrementing.");
            cache.SetCounter("mycounter", 9000);
            ulong? counter = cache.GetCounter("mycounter");
            if (counter.HasValue) {
                Console.Out.WriteLine("Fetched mycounter, value: " + counter.Value);
            }

//Increment the counter
            counter = cache.Increment("mycounter", 1);
            if (counter.HasValue) {
                Console.Out.WriteLine("Incremented mycounter with 1, new value: " + counter.Value);
            }

//Decrement the counter
            counter = cache.Decrement("mycounter", 9000);
            if (counter.HasValue) {
                Console.Out.WriteLine("Decremented mycounter with 9000, new value: " + counter.Value);
            }

//Append and prepend
            Console.Out.WriteLine("Storing bar for append/prepend");
            cache.Set("foo", "bar");
            Console.Out.WriteLine("Appending baz");
            cache.Append("foo", " baz");
            Console.Out.WriteLine("Prepending foo");
            cache.Prepend("foo", "foo ");
            Console.Out.WriteLine("New value: " + cache.Get("foo"));

//Cas
            cache.Delete("castest");
            Console.Out.WriteLine("Trying to CAS non-existant key castest: " + cache.CheckAndSet("castest", "a", 0));
            Console.Out.WriteLine("Setting value for key: castest, value: a");
            cache.Set("castest", "a");
            Console.Out.WriteLine("Trying to CAS key castest with the wrong unique: " + cache.CheckAndSet("castest", "a", 0));
            ulong unique;
            cache.Gets("castest", out unique);
            Console.Out.WriteLine("Getting cas unique for key castest: " + unique);
            Console.Out.WriteLine("Trying to CAS again with the above unique: " + cache.CheckAndSet("castest", "b", unique));
            string value = cache.Gets("castest", out unique) as string;
            Console.Out.WriteLine("New value: " + value + ", new unique: " + unique);

Console.Out.WriteLine("Displaying the socketpool status:");
            foreach (KeyValuePair<string, Dictionary<string, string>> host in cache.Status()) {
                Console.Out.WriteLine("Host: " + host.Key);
                foreach (KeyValuePair<string, string> item in host.Value) {
                    Console.Out.WriteLine("\t" + item.Key + ": " + item.Value);
                }
                Console.Out.WriteLine();
            }

Console.Out.WriteLine();
            Console.Out.WriteLine("Finished. Press enter to exit.");
            Console.In.ReadLine();
        }
    }

}

注:memcached是以KEY-VALUE的方式进行数据存储的,KEY的大小限制:Key(max)<=250个字符;VALUE在存储时有 限制:Value(max)<= 1M;memcached默认过期时间:ExpiresTime(max)= 30(days)。

转载于:https://www.cnblogs.com/yuanxiaoping_21cn_com/archive/2012/04/18/2456186.html

memcache/memcached/memcachedb 配置、安装(转)相关推荐

  1. memcached的基本命令(安装、卸载、启动、配置相关)

    memcached的基本命令(安装.卸载.启动.配置相关): -p 监听的端口  -l 连接的IP地址, 默认是本机   -d start 启动memcached服务  -d restart 重起me ...

  2. php5.4之分布式缓存memcache(windows7下安装配置)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qq1355541448/article/details/36663203 使用理由:就是为了频繁查询 ...

  3. win7 64位系统 memcache/memcached安装

    2019独角兽企业重金招聘Python工程师标准>>> memcached介绍: Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过 ...

  4. win7 安装 wamp2.5版本的memcache+memcached

    1. 下载  memcache+memcached 2. 以管理员身份运行 cmd.exe,并转至memcached所在文件夹,比如: cd c:\memcached .(如果不以管理员身份运行,将得 ...

  5. ubuntu php7 memcache,linux ubuntu下安装php memcache扩展

    memcached 安装 sudo apt-get install memcached memcached 参数说明 memcached -d -m 50 -p 11211 -u root -m 指定 ...

  6. memcached 的Linux安装

    一.memcached 的编译安装 准备: Memcached下载地址:http://www.danga.com/memcached/ libevent下载地址: http://monkey.org/ ...

  7. memcached介绍,安装与基本使用

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据库驱动网站的速度.Memcached ...

  8. 宝塔无法安装php memcached,宝塔面板安装Memcached缓存加速wordpress

    开启缓存是为了加速wordpress经常用到的一种提速方法,除了有专门的插件进行静态化缓存外,还可以使用Memcached进行内存缓存,宝塔面板集成了Memcached,大大降低了使用难度.今天就记录 ...

  9. Memcached windows 下安装与应用

    Memcached windows 下安装与php应用 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数 ...

最新文章

  1. 用Ajax构建关键任务的企业级Web应用 ——《深入Ajax:架构与最佳实践》
  2. 在Spring、Hibernate中使用Ehcache缓存
  3. thinkphp3.2.3 自定义路由实践
  4. 【转载】 C++中回车换行(\n\r)和换行(\r)的区别
  5. mysql group和order_mysql 用 group by 和 order by同时使用
  6. mysql截取字符串最后两位_MySQL字符串函数substring:字符串截取
  7. 手把手带你入门Python爬虫(二、爬虫预备知识)
  8. asp.net 开发知识小结【转】
  9. 网页右下角3秒自动弹出悬浮在线客服代码
  10. YOLOv5网络详解
  11. curl常用命令的使用
  12. java中osend_Java中OIO与NIO的简单区别
  13. 【财富空间】一个人彻底的改变 一定始于内心的改变
  14. 西电2020计算机考研,西安电子科技大学研究生院,西电2020年考研成绩最新信息!...
  15. x264中码率控制(一)
  16. wordPress数据结构 数据库中的表、字段、类型及说明
  17. 手机h5实现长按复制(支持安卓和ios)
  18. MinIO下载和MinIO中国镜像地址
  19. ucore lab1 实验报告
  20. 【双11狂欢大促】实验室4年来最大折扣,羊毛提前褥,错过再等一年!

热门文章

  1. pom添加依赖后不报错但是代码依然缺少依赖
  2. Types cannot be provided in put mapping requests, unless the include_type_name parameter is set to t
  3. superset可视化-Bar Chart
  4. Adaboost算法原理分析和实例+代码(转载)
  5. 开发使用air还是pro_苹果MacBook全系选购指北,Air和Pro如何选?
  6. AI没有偏见?它们从人类的语言中学会了性别和种族歧视
  7. OAuth2.0 授权的工作原理
  8. python 知识 rstrip,strip,lstrip
  9. Javascript权威指南——第一章Javascript概述
  10. NYOJ 1068 ST(段树 为段更新+间隔总和)