Linux下快取服務器的應用

來源:互聯網
上載者:User
摘要:由於資料庫儲存的資料量越來越大,查詢速度也就變的越來越慢,因此就有了快取服務器應用的必要,本文是介紹Memcached的安裝以及簡單的使用。

本文只介紹memcached的PHP的API,想查看其他關於Memcached的API文檔案,請訪問 http://www.danga.com/memcached/

目錄

    一、環境需求
    二、下載相關軟體
    三、安裝和配置

     

      1、安裝Memcached
      2、安裝memcache PHP模組
      3、測試指令碼

    四、關於本文


++++++++++++++++++++++++++++++++++++++++
本文
++++++++++++++++++++++++++++++++++++++++

一、環境需求
安裝Memcached需要libevent庫的支援,所以請在安裝Memcached之前檢查有沒有安裝libevent。測試環境還需要PHP的支援,本文假設PHP已經安裝到/usr/local/php目錄下,也就是在編譯PHP的時候使用perfix參數指定目錄(--prefix=/usr/local/php)

二、下載相關軟體

Memcached:http://www.danga.com/memcached/
memcache PHP模組: http://pecl.php.net/package/memcache 推薦使用1.5版
libevent : http://www.monkey.org/~provos/libevent/

本文不再講述如何安裝libevent

三、安裝和配置

1、安裝Memcached

root@tonyvicky:# tar vxzf memcached-1.1.12.tar.gz
root@tonyvicky:# cd memcached-1.1.12
root@tonyvicky:# ./configure --prefix=/usr/local/memcached
root@tonyvicky:# make
root@tonyvicky:# make install

 

安裝完之後要啟動服務

root@tonyvicky:# cd /usr/local/memcached/bin
root@tonyvicky:# ./memcached -d -m 50 -p 11211 -u root

 

參數說明 -m 指定使用多少兆的緩衝空間;-p 指定要監聽的連接埠; -u 指定以哪個使用者來運行

2、安裝memcache PHP模組

root@tonyvicky:# tar vxzf memcache-1.5.tgz
root@tonyvicky:# cd memcache-1.5
root@tonyvicky:# /usr/local/php/bin/phpize
root@tonyvicky:# ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir
root@tonyvicky:# make
root@tonyvicky:# make install

 

安裝完後會有類似這樣的提示:

Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20050922/

 

把這個記住,然後修改php.ini,把

extension_dir = "./"

 

修改為

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20050922/"

 

並添加一行

extension=memcache.so

 

3、測試指令碼

自己寫一個PHP程式測試一下吧

<?php
$memcache = new Memcache; //建立一個memcache對象
$memcache->connect('localhost', 11211) or die ("Could not connect"); //串連Memcached伺服器
$memcache->set('key', 'test'); //設定一個變數到記憶體中,名稱是key 值是test
$get_value = $memcache->get('key'); //從記憶體中取出key的值
echo $get_value;
?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.