在Windows中安裝Memcached

来源:http://www.cnblogs.com/jaxu/archive/2016/02/18/5196811.html
-Advertisement-
Play Games

Memcached是一個高併發的記憶體鍵值對緩存系統,它的主要作用是將資料庫查詢結果,內容,以及其它一些耗時的計算結果緩存到系統記憶體中,從而加速Web應用程式的響應速度。 Memcached最開始是作為Linux應用程式被安裝在Linux伺服器上來使用的,不過自從開源之後,它又被重新編譯以適用於Win


  Memcached是一個高併發的記憶體鍵值對緩存系統,它的主要作用是將資料庫查詢結果,內容,以及其它一些耗時的計算結果緩存到系統記憶體中,從而加速Web應用程式的響應速度。

  Memcached最開始是作為Linux應用程式被安裝在Linux伺服器上來使用的,不過自從開源之後,它又被重新編譯以適用於Windows環境。JellycanNorthscale兩個站點都提供了Windows的二進位可執行文件下載,下麵是下載的地址:

  http://code.jellycan.com/files/memcached-1.2.5-win32-bin.zip

  http://code.jellycan.com/files/memcached-1.2.6-win32-bin.zip

  http://downloads.northscale.com/memcached-win32-1.4.4-14.zip

  http://downloads.northscale.com/memcached-win64-1.4.4-14.zip

  http://downloads.northscale.com/memcached-1.4.5-x86.zip

  http://downloads.northscale.com/memcached-1.4.5-amd64.zip

  在1.4.5版本之前,memcached可以被安裝成一個服務,但之後的版本中該功能被移除了。因此memcached的安裝可以分為兩類,第一類是1.4.5之前的版本,另一類是1.4.5之後的版本。

安裝memcached < 1.4.5:

  1. 將下載的文件解壓到任意目錄。

  2. 1.4.5之前版本的memcached會被安裝成一個服務,以administrator打開控制台,運行下麵的命令:

c:\memcached\memcached.exe -d install

* 註意將路徑c:\memcached\memcached.exe替換成你本地的安裝路徑。

  3. 然後使用下麵的命令啟動或停止memcached服務:

c:\memcached\memcached.exe -d start
c:\memcached\memcached.exe -d stop

  4. 通過註冊表鍵值來修改memcached的配置項。在運行中輸入regedit.exe,然後導航到"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached"。修改其中的鍵值。例如你想增加memcached所使用的最大記憶體限制,可以修改ImagePath的值:

"c:\memcached\memcached.exe" -d runservice -m 512

* 除了參數'-m 512'之外,你還可以使用其它的參數。通過“c:\memcached\memcached.exe -h”可以查看所有能使用的參數。

  5. 如果要卸載memcached服務,可以使用下麵的命令:

c:\memcached\memcached.exe -d uninstall

 安裝memcached >= 1.4.5

  1. 將下載的文件解壓到任意目錄。

  2. 1.4.5之後版本的memcached不能作為Windows服務來運行,必須使用Windows計劃任務來運行它。要將memcached配置成當Windows啟動時自動運行,在命令行運行下麵的命令:

schtasks /create /sc onstart /tn memcached /tr "'c:\memcached\memcached.exe' -m 512"

* 註意將路徑c:\memcached\memcached.exe替換成你本地的安裝路徑。

** 除了參數'-m 512'之外,你還可以使用其它的參數。通過“c:\memcached\memcached.exe -h”可以查看所有能使用的參數。

  3. 通過下麵的命令將memcached從Windows計劃任務中移除:

schtasks /delete /tn memcached

 

在PHP中使用memcached

  要在PHP中使用memcached,首先需要安裝memcache擴展包:

  1. 查看你本地的PHP擴展包目錄里是否有php_memcache.dll這個文件,如果沒有,從https://pecl.php.net/package/memcache下載(選擇windows dll文件),然後複製到PHP擴展包目錄里。

  2. 在php.ini中添加下麵的代碼以啟用memcache擴展包:

extension=php_memcache.dll

  3. 創建下麵的php示例代碼進行測試:

<?php

$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");

$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>\n";

$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;

$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";

$get_result = $memcache->get('key');
echo "Data from the cache:<br/>\n";

var_dump($get_result);

?> 

 

在Python中使用memcached

  要在Python中使用memcached,首先需要安裝memcached客戶端:

  1. 執行下麵的命令進行memcached客戶端的安裝。第一個適用於Python 2.x,第二個適用於Python 3.x。

pip install python-memcached
pip install python3-memcached

  2. 創建下麵的python示例代碼進行測試:

import memcache
mc = memcache.Client(['127.0.0.1:11211'], debug=0)
mc.set("some_key", "Some value")
value = mc.get("some_key")
mc.set("another_key", 3)
mc.delete("another_key")
mc.set("key", "1")   # note that the key used for incr/decr must be a string.
mc.incr("key")
mc.decr("key")

 

在Node.js中使用memcached

  在Node.js中可以通過memcached包來使用memcache,Github的地址https://github.com/3rd-Eden/memcached。下麵是一段示例代碼:

var Memcached = require( 'memcached' );

// connect to our memcached server on host 10.211.55.5, port 11211
var memcached = new Memcached( "10.211.55.5:11211" );

memcached.set( "hello", 1, 10000, function( err, result ){
    if( err ) console.error( err );
    
    console.dir( result );
    memcached.end(); // as we are 100% certain we are not going to use the connection again, we are going to end it
});

memcached.get( "hello", function( err, result ){
    if( err ) console.error( err );
    
    console.dir( result );
    memcached.end(); // as we are 100% certain we are not going to use the connection again, we are going to end it
});

   更詳細的使用方法可以參考Githun上的說明。

 

Memcached的數據統計

  要查看memcached的數據統計,可以通過telnet連接到memcached:

telnet 127.0.0.1 11211

* ip地址後面的數字為memcached運行的埠號

** 在Windows 10中telnet組件預設並沒有添加,可通過Control Panel > Programs and Features > Turn Windows features on or off來添加。

  然後使用stats來查看統計信息。下表列出了stats統計結果中各數據項的含義:

NameTypeMeaning
pid 32u Process id of this server process
uptime 32u Number of secs since the server started
time 32u current UNIX time according to the server
version string Version string of this server
pointer_size 32 Default size of pointers on the host OS (generally 32 or 64)
rusage_user 32u.32u Accumulated user time for this process (seconds:microseconds)
rusage_system 32u.32u Accumulated system time for this process (seconds:microseconds)
curr_items 32u Current number of items stored
total_items 32u Total number of items stored since the server started
bytes 64u Current number of bytes used to store items
curr_connections 32u Number of open connections
total_connections 32u Total number of connections opened since the server started running
connection_structures 32u Number of connection structures allocated by the server
reserved_fds 32u Number of misc fds used internally
cmd_get 64u Cumulative number of retrieval reqs
cmd_set 64u Cumulative number of storage reqs
cmd_flush 64u Cumulative number of flush reqs
cmd_touch 64u Cumulative number of touch reqs
get_hits 64u Number of keys that have been requested and found present
get_misses 64u Number of items that have been requested and not found
delete_misses 64u Number of deletions reqs for missing keys
delete_hits 64u Number of deletion reqs resulting in an item being removed.
incr_misses 64u Number of incr reqs against missing keys.
incr_hits 64u Number of successful incr reqs.
decr_misses 64u Number of decr reqs against missing keys.
decr_hits 64u Number of successful decr reqs.
cas_misses 64u Number of CAS reqs against missing keys.
cas_hits 64u Number of successful CAS reqs.
cas_badval 64u Number of CAS reqs for which a key was found, but the CAS value did not match.
touch_hits 64u Numer of keys that have been touched with a new expiration time
touch_misses 64u Numer of items that have been touched and not found
auth_cmds 64u Number of authentication commands handled, success or failure.
auth_errors 64u Number of failed authentications.
evictions 64u Number of valid items removed from cache to free memory for new items
reclaimed 64u Number of times an entry was stored using memory from an expired entry
bytes_read 64u Total number of bytes read by this server from network
bytes_written 64u Total number of bytes sent by this server to network
limit_maxbytes 32u Number of bytes this server is allowed to use for storage.
threads 32u Number of worker threads requested. (see doc/threads.txt)
conn_yields 64u Number of times any connection yielded to another due to hitting the -R limit.
hash_power_level 32u Current size multiplier for hash table
hash_bytes 64u Bytes currently used by hash tables
hash_is_expanding bool Indicates if the hash table is being grown to a new size
expired_unfetched 64u Items pulled from LRU that were never touched by get/incr/append/etc before expiring
evicted_unfetched 64u Items evicted from LRU that were never touched by get/incr/append/etc.
slab_reassign_running bool If a slab page is being moved
slabs_moved 64u Total slab pages moved
crawler_reclaimed 64u Total items freed by LRU Crawler
lrutail_reflocked 64u Times LRU tail was found with active ref. Items moved to head to avoid OOM errors.

  更詳細的內容可以查看這裡:https://github.com/memcached/memcached/blob/master/doc/protocol.txt

  另外,有關Memcached的常用命令及使用說明也可以參考這篇文章:http://www.cnblogs.com/jeffwongishandsome/archive/2011/11/06/2238265.html


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 分類:C#、Android、VS2015; 創建日期:2016-02-18 1、主界面運行截圖 2、MainActivity.cs文件中對應的代碼 chItems.Add(new Chapter() { ChapterName = "第10章 擴展組件庫和其他視圖", ChapterItems = ...
  • 博客園第三方客戶端-i博客園正式發佈App Store 1. 前言 算來從15年8月到現在自學iOS已經快7個月了,雖然中間也是斷斷續續的,不過竟然堅持下來了。年後要找實習啦,於是萌生了一個想法 —— 寫一個app練練手。這次我沒弄後臺了,直接使用了博客園的open api(嘿嘿)。之前也做過一個a
  • 代碼: RootViewController.m #import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (id)initWithNibName
  • 主要是對項目中用過的 oracle 函數進行總結,並做出目錄,方便後續項目是快速查找,提高效率。 01.Round (數值的四捨五入) 描述:傳回一個數值,該數值是按照指定的小數位元數進行四捨五入運算的結果。 SELECT Round(Number,[Decimal_Places])FROM Dua
  • 2張數據表:訂單Order,訂單進度OrderProgress 設計思路一、 1.Order:oid,userid,postInfo,isDel 2.OrderProgress:opId,oid,createTime,PayTime,isDel 這麼設計,oid做外鍵,當拿到一個Order對象,在M
  • 有時,為了讓應用程式運行得更快,所做的全部工作就是在這裡或那裡做一些很小調整。但關鍵在於確定如何進行調整!遲早您會遇到這種情況:應用程式中的 SQL 查詢不能按照您想要的方式進行響應。它要麼不返回數據,要麼耗費的時間長得出奇。如果它降低了企業應用程式的速度,用戶必須等待很長時間。用戶希望應用程式響應
  • 概述 本章節主要介紹配置HaProxy+Keepalived高可用群集,Mycat的配置就不在這裡做介紹,可以參考我前面寫的幾篇關於Mycat的文章。 部署圖: 配置 HaProxy安裝 181和179兩台伺服器安裝haproxy的步驟一致 --創建haproxy用戶 useradd haproxy
  • 最近看到有部分人MongoDB安裝之後總是啟動不起來,在這裡,寫了一個簡單的搭建教程 直接進入正題 1.mongoDB下載地址 https://www.mongodb.org/downloads#production 2.安裝 預設安裝目錄在 C:\Program Files\MongoDB\ 中,
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...