Memcached windows 下安装与php应用

Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。但是它并不提供冗余(例如,复制其hashmap条目);当某个服务器S停止运行或崩溃了,所有存放在S上的键/值对都将丢失。

Memcached官方:http://danga.com/memcached/

关于Memcached的介绍请参考:Memcached深度分析

下载Windows的Server端

下载地址:http://code.jellycan.com/memcached/

安装Memcache Server(也可以不安装直接启动)

1. 下载memcached的windows稳定版,解压放某个盘下面,比如在c:\memcached
2. 在CMD下输入 “c:\memcached\memcached.exe -d install” 安装.
3. 再输入:”c:\memcached\memcached.exe -d start” 启动。NOTE: 以后memcached将作为windows的一个服务每次开机时自动启动。这样服务器端已经安装完毕了。

如果下载的是二进制的版本,直接运行就可以了,可以加上参数来加以设置。
常用设置:
-p <num>          监听的端口
-l <ip_addr>      连接的IP地址, 默认是本机
-d start          启动memcached服务
-d restart        重起memcached服务
-d stop|shutdown  关闭正在运行的memcached服务
-d install        安装memcached服务
-d uninstall      卸载memcached服务
-u <username>     以<username>的身份运行 (仅在以root运行的时候有效)
-m <num>          最大内存使用,单位MB。默认64MB
-M                内存耗尽时返回错误,而不是删除项
-c <num>          最大同时连接数,默认是1024
-f <factor>       块大小增长因子,默认是1.25
-n <bytes>        最小分配空间,key+value+flags默认是48
-h                显示帮助

然后就可以用php的memcached客户端来试一下了。

Php代码与memcached的交互和与mysql的交互原理是一样的,需要安装一个服务器端的memcached ,现有的交互处理过程已经封装成了一个php的扩展了;需要在php.ini中,将这个扩展加进去。

Php memcached官方手册地址:http://cn2.php.net/manual/en/memcached.get.php

php扩展库pecl下载地址:

http://museum.php.net/php5/

配置:

1.    下载pecl模块包(地址如上),解压后将php_memache.dll放到php目录的ext子目录下,为了使得能正常使用,最好下载和php版本一致的模块包。

2.    在php.ini文件中导入’extension=php_memcache.dll’

然后重启apache,估计就可以了(可以在phpinfo.php 中,看看是否有memcached模块),当然了,我们可以写一个实验一下

$memcache_obj = new Memcache;

$memcache_obj->connect(‘localhost’, 11211);

$memcache_obj->set(‘var_key’, ’This is a memcached test!’,MEMCACHE_COMPRESSED, 50);

echo $memcache_obj->get(‘var_key’);

例举一些常用的Memcache方法:

Memcache::add // 添加一个值,如果已经存在,则返回false

Memcache::addServer // 添加Memcache地址

Memcache::close // 关闭一个Memcache的连接

Memcache::connect // 打开一个到Memcache的连接

Memcache::decrement // 对保存的某个key中的值进行减法操作

Memcache::delete // 删除一个Memcache上的key值

Memcache::flush // 刷新所有Memcache上保存的项目(类似于删除所有的保存的项目)

Memcache::get // 从Memcache上获取一个key值

Memcache::getExtendedStats // 获取进程池中所有进程的运行系统统计

Memcache::getServerStatus // 获取运行服务器的参数

Memcache::getStats //获取当前Memcache服务器运行的状态

Memcache::getVersion // 返回运行的Memcache的版本信息

Memcache::increment // 对保存的某个key中的值进行加法操作

Memcache::pconnect // 打开一个到Memcache的长连接

Memcache::replace // 替换一个已经存在Memcache服务器上的项目(功能类似Memcache::set)

Memcache::set // 向Memcache添加一个值,如果已经存在,则覆写

Memcache::setCompressThreshold // 对大于某一大小的数据进行压缩

Memcache::setServerParams // 在运行时修改服务器的参数

一个简单的用法案例:

<?php$mem = new Memcache;$mem->connect("127.0.0.1",12000);//Memcache::set方法有四个参数,第一个参数是key,第二个参数是value,第三个参数可选,表示是否压缩保存,第四个参数可选,用来设置一个过期自动销毁的时间。$mem->set('test','123',0,60);//Memcache::add方法的作用和Memcache::set方法类似,区别是如果 Memcache::add方法的返回值为false,表示这个key已经存在,而Memcache::set方法则会直接覆写。$mem->add('test','123',0,60);//Memcache::get方法的作用是获取一个key值,Memcache::get方法有一个参数,表示key。$mem->get('test');//输出为'123'//Memcache::replace 方法的作用是对一个已有的key进行覆写操作,Memcache::replace方法有四个参数,作用和Memcache::set方法的相同。$mem->replace('test','456',0,60);//Memcache::delete方法的作用是删除一个key值,Memcache::delete方法有两个参数,第一个参数表示key,第二个参数可选,表示删除延迟的时间。$mem->delete('test',60);?>

.net-memcached客户端用法:

C# 下可用的API(每个客户端API中都有详细的说明和注释)

https://sourceforge.net/projects/memcacheddotnet/
http://www.codeplex.com/EnyimMemcached/ - Client developed in .NET 2.0 keeping performance and extensibility in

mind. (Supports consistent hashing.) 
http://code.google.com/p/beitmemcached/ - Client developed by BeIT with many new features

转载出处: http://www.yaosansi.com/

----------------------------------------------------------------------------------------

Client调用:

下载示例代码网址: http://sourceforge.net/projects/memcacheddotnet/

C#/.NET memcached client library. This library can be used by .NET projects to access memcached servers. Ported from the Java memcached library located athttp://www.whalin.com/memcached/.

e.g.:

View Code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
 * MemcachedBench.cs
 *
 * Copyright (c) 2005
 * Tim Gebhardt <tim@gebhardtcomputing.com>
 
 * Based off of code written by
 * Greg Whalin <greg@meetup.com>
 * for his Java Memcached client:
 * http://www.whalin.com/memcached/
 
 *
 * See the memcached website:
 * http://www.danga.com/memcached/
 *
 * This module is Copyright (c) 2005 Tim Gebhardt.
 * All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later
 * version.
 *
 * This library is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * @author Tim Gebhardt<tim@gebhardtcomputing.com> 
 * @version 1.0
 */
namespace Memcached.MemcachedBench
{
    using System;
    using System.Collections;
  
    using Memcached.ClientLibrary;
  
    public class MemcachedBench 
    {
        /// <summary>
        /// Arguments: 
        ///     arg[0] = the number of runs to do
        ///     arg[1] = the run at which to start benchmarking
        /// </summary>
        /// <param name="args"></param>
        [STAThread]
        public static void Main(String[] args) 
        {
            int runs = 100;
            int start = 200;
            if(args.Length > 1)
            {
                runs = int.Parse(args[0]);
                start = int.Parse(args[1]);
            }
  
            //可以设置多个服务器列表
            //string[] serverlist = { "127.0.0.1:11211" , "140.192.34.73:11211" };
            string[] serverlist = { "127.0.0.1:11211" };    //, "140.192.34.73:11211" };
  
            // initialize the pool for memcache servers
            SockIOPool pool = SockIOPool.GetInstance();
            pool.SetServers(serverlist);
  
            pool.InitConnections = 3;
            pool.MinConnections = 3;
            pool.MaxConnections = 5;
  
            pool.SocketConnectTimeout = 1000;
            pool.SocketTimeout = 3000;
  
            pool.MaintenanceSleep = 30;
            pool.Failover = true;
  
            pool.Nagle = false;
            pool.Initialize();
  
            // initialize the pool for memcache servers
//          SockIOPool pool = SockIOPool.Instance;
//          pool.Servers = serverlist;
//
//          pool.InitConn = 5;
//          pool.MinConn = 5;
//          pool.MaxConn = 50;
//          pool.MaintSleep = 30;
//          pool.SocketTO = 1000;
//
//          pool.Nagle = false;
//          pool.Initialize();
  
//
//          // get client instance
            MemcachedClient mc = new MemcachedClient();
            mc.EnableCompression = false;
  
//          MemcachedClient mc = new MemcachedClient();
//          mc.CompressEnable = false;
//          mc.CompressThreshold = 0;
//          mc.Serialize = true;
  
            string keyBase = "testKey";
            string obj = "这是我的字符串This is a test of an object blah blah es, serialization does not seem to slow things down so much.  The gzip compression is horrible horrible performance, so we only use it for very large objects.  I have not done any heavy benchmarking recently";
  
            long begin = DateTime.Now.Ticks;
            for(int i = start; i < start+runs; i++) 
            {
                mc.Set(keyBase + i, obj);
            }
            long end = DateTime.Now.Ticks;
            long time = end - begin;
  
            Console.WriteLine(runs + " 设置花费的时间-sets: " + new TimeSpan(time).ToString() + "ms");
  
            begin = DateTime.Now.Ticks;
            int hits = 0;
            int misses = 0;
            for(int i = start; i < start+runs; i++) 
            {
                string str = (string) mc.Get(keyBase + i);
                Console.WriteLine("key={0},value={1}",keyBase+i,str);
                if(str != null)
                    ++hits;
                else
                    ++misses;
            }
            end = DateTime.Now.Ticks;
            time = end - begin;
  
            Console.WriteLine(runs + "读取花费的时间- gets: " + new TimeSpan(time).ToString() + "ms");
            Console.WriteLine("Cache hits,成功: " + hits.ToString());
            Console.WriteLine("Cache misses,失败: " + misses.ToString());
  
            IDictionary stats = mc.Stats();
            foreach(string key1 in stats.Keys)
            {
                Console.WriteLine(key1);
                Hashtable values = (Hashtable)stats[key1];
                foreach(string key2 in values.Keys)
                {
                    Console.WriteLine(key2 + ":" + values[key2]);
                }
                Console.WriteLine();
            }
  
            SockIOPool.GetInstance().Shutdown();
            Console.ReadKey();
        }
    }
}

服务器端: http://files.cnblogs.com/wucg/memcached-1.2.6-win32-bin.zip

下载Client库文件及示例,vs2008,.netframework 1.0,2.0 http://files.cnblogs.com/wucg/clientlib.zip

http://www.splinedancer.com/memcached-win32/

memcached for Windows

This is a port of memcached to the win32 architecture byKenneth Dalgleish, based on Kronuz's 1.2.1 port.This port is not supported by the official memcached team.

Install

The win32 version of memcached can be run both as a NT Service or from the command line.To install memcached as a service, follow the next steps:

  1. Unzip the binaries in your desired directory (eg. c:\memcached)
  2. Install the service using the command: 'c:\memcached\memcached.exe -d install' from the command line
  3. Start the server from the Microsoft Management Console or by running the following command: 'c:\memcached\memcached.exe -d start'
  4. Use the server, by default listening to port 11211

Building from source

To build from source, you will need Visual Studio 2005 (any edition with C++ should work), Windows SDK (eg.Windows SDK for Windows Server 2008 and .NET Framework 3.5) and libevent (win32 binary provided on this page).

  1. Install Visual Studio 2005
  2. Install Windows SDK
  3. Put libevent.lib in Win32-Prj/ folder
  4. Open solution file and it should build

Downloads

memcached 1.2.4 Win32 Beta

  • memcached 1.2.4 Win32 Beta Binaries (09.03.2008)
  • memcached 1.2.4 Win32 Beta Source (09.03.2008)
  • memcached 1.2.4 Win32 Beta Patch for SVN revision 662 (tag 1.2.4) (09.03.2008)

Libevent 1.3e Win32

(Needed if building from source)

  • Libevent 1.3e Win32 Binary (06.03.2008)

PHPUnit 需要测试memcache类的方法  

由于zend for eclipse6.1.0 没有php_memcache.dll这个动态链接库,我们在phpunit测试的时候会报找不到php_memcache类的错,于是只需要把php_memcache.dll(对应版本的:php_memcache.dll)放入:D:\Program Files\Zend\Zend Studio for Eclipse - 6.1.0\plugins\org.zend.php.debug.debugger.win32.x86_5.2.14.v20080602\resources\php5\ext\php-cgi.exe,然后修改D:\Program Files\Zend\Zend Studio for Eclipse - 6.1.0\plugins\org.zend.php.debug.debugger.win32.x86_5.2.14.v20080602\resources\php5\php.ini   加入一行:extension=php_memcache.dll  ,重新启动zend for eclipse6.1.0即可!

Memcached windows 下安装与应用相关推荐

  1. Memcached学习---(3)Windows 下安装 Memcached

    Windows 下安装 Memcached 官网上并未提供 Memcached 的 Windows 平台安装包,我们可以使用以下链接来下载,你需要根据自己的系统平台及需要的版本号点击对应的链接下载即可 ...

  2. Windows 下安装 Memcached

    Windows 下安装 Memcached 在官网下载安装包解压到无中文目录的文件目录下, 在 memcached1.4.5 版本之后,memcached 不能作为服务来运行,需要使用任务计划中来开启 ...

  3. windows下安装各个版本memcache扩展

    这篇文章主要介绍了windows下安装php5.2.,php5.3.,php5.4.*版本的memcache扩展,需要的朋友可以参考下 注:如使用集成环境成功率低,请自行配置php apache,表示 ...

  4. mysql5.7.25安装包,Mysql5.7.25在windows下安装

    在网上看到了很多安装方法,也试了很多,md,网上资源多了也是有各种坑,这里只说在windows下安装mysql5.7.25 一.下载安装包 下载后解压到自己想要安装的目录,我的是:D:\MYSQL\ ...

  5. 基于svnserve的SVN服务器(windows下安装与配置)

    基于svnserve的SVN服务器(windows下安装与配置) 关键字: svn 安装SVNserve 从http://subversion.tigris.org/servlets/ProjectD ...

  6. Windows下安装Z3的Python3版

    文章目录 Windows下安装Z3的Python3版 pip 安装(不推荐,很慢) 使用微软官方构建好的DLL(推荐,快速) Windows下安装Z3的Python3版 GitHub官方仓库地址:Z3 ...

  7. 在windows下安装concurrentlua

    concurrentlua的makefile只提供了unix下的版本,如果直接按make里面得拷贝路径安排文件 在windows下是无法凑效的.这里我把我在windows下安装concurrentlu ...

  8. linux/windows下安装scala

    为什么80%的码农都做不了架构师?>>>    一.linux下安装scala 1.保证jdk安装成功,版本在1.5或者更改版本,java和javac均可用. 2.官网下载scala ...

  9. windows下安装cygwin及配置

    windows下安装cygwin及配置 对于使用Windows操作系统作为开发平台同时又喜欢类unix环境的朋友(Windows不是最方便的开发环境),这里是在Cygwin环境下安装Rails的步骤 ...

最新文章

  1. MySQL 的 Binlog 日志处理工具(Canal/Maxwell/Databus/DTS)对比
  2. 赛道一出,今后无需再熬夜
  3. bootsect.s 预备——Linux-0.11 剖析笔记(一)
  4. user_all_tables,user_tables等视图的说明
  5. python安装后怎样配解释器_python解释器安装教程以及环境变量的配置
  6. springMVC带文件的表单数据无法绑定到参数中
  7. weakhashmap_Java WeakHashMap values()方法与示例
  8. 分布式锁用Redis坚决不用Zookeeper?
  9. centos 6.5上安装php7,centos 6.5 编译安装PHP7
  10. Facebook Cache Token Issue
  11. win7开启telnet工具
  12. NVIDIA助力风暴英雄黄金世俱杯Ballistix强势夺冠
  13. android 画爱心进度条_android自定义圆形进度条,实现动态画圆效果
  14. 【Laravel系列4.4】模型Eloquent ORM的使用(二)
  15. 计算机技术在建筑学的应用论文,计算机应用于建筑设计中的影响的论文
  16. Cornerstone清除缓存
  17. 计算机体系-指令系统
  18. 通过iptable进行流量转发
  19. 【小实验1】比较ResNet、ViT、SwinTransformer的归纳偏置(然而并没有达到预期结果)
  20. Android实现全景图

热门文章

  1. linux mysql ssh通道_通过SSH通道来访问MySQL
  2. 洛谷——P1161 开灯
  3. CSS3实现卡片翻转动画
  4. 如何将mysql中的表传到elipse中_eclipse怎么连接到MySQL中的表!!!!!
  5. OpenCV 图像金字塔buildPyramid、pyrDown、pyrUp
  6. Please review your Gradle project setup in the android/ folde
  7. preact源码学习(3)
  8. jupyter kernel添加使用和配置
  9. Shell脚本的模块化和脚本复用
  10. 【Computer Organization笔记23】非易失性存储:磁表面存储设备,磁盘的访问过程,RAID技术