簡體   English   中英

如何在Windows中為PHP安裝和使用memcached?

[英]How to install and use memcached in Windows for PHP?

我在Windows 7中安裝了memcached二進制文件並將其作為服務器啟動。

當我輸入wmic進程獲取描述時,exetuablepath | findstr memcached.exe我得到了響應:命令行上的memcached.exe c:\\ memcached \\ memcached.exe。

當我嘗試在php.net上運行示例代碼時,我上了我的瀏覽器:

致命錯誤:第3行的C:\\ DocumentRoot \\ Framework \\ index.php中找不到類“Memcache”調用堆棧:0.0010 335928 1. {main}()C:\\ DocumentRoot \\ Framework \\ index.php:0

那么,我做錯了什么呢? 我正在使用memcache.dll,因為我認為Windows不存在memcached.dll?

任何遇到使用memcached在Windows上工作的問題的人的注意事項。

  • 對於初學者,請確保您擁有正確版本的memcached dll並且可以訪問它。 http://windows.php.net/downloads/pecl/releases/memcache/3.0.8/提供了廣泛的選擇,選擇錯誤版本的memcached非常容易!
  • 如果您運行的是PHP 5.5,則還需要php5.dll。 你可以在這里得到這個
  • 您可能需要編輯環境PATH設置,以便找到此dll。 轉到“我的電腦” - >“屬性” - >“高級”,然后單擊“環境變量”以查看/編輯路徑。 如果您編輯它,則需要重新啟動計算機。
  • 確保已安裝memcached服務器 按Ctrl + Alt + Del並檢查memcached是否存在於您的服務列表中
  • 如果不是,你需要管理員運行的Cmd提示符安裝它 (從開始菜單,選擇附件,單擊命令提示符並選擇以管理員身份運行)c:\\ pathtomemcached \\ memcached.exe -d install
  • 使用c:\\ pathtomemcached \\ memcached.exe -d start或net start“memcached Server”執行此操作。 在我的安裝上,前者不起作用
  • 同樣,我無法從任務管理器的“服務”選項卡啟動memcached
  • 能夠在低級別使用memcached是很方便的,因此如果需要, 啟用telnet ,並從命令提示符鍵入telnet。 現在打開端口11211並嘗試使用memcached
  • 能夠密切關注memcached中發生的事情也很有用。 phpMemCacheAdmin是迄今為止最好的工具

根據評論,我假設您沒有下載並安裝memcached,但已成功安裝了PHP的memcached模塊。 基本上,你已經拿到了車鑰匙,但沒有車。

memcached是為Linux而構建的,但它已被其他人移植到Windows。 這個教程很舊,但它可能正是你要找的東西: http//www.codeforest.net/how-to-install-memcached-on-windows-machine

這是為了未來的游客!

  1. 檢查phpinfo()並查看它是否已列出。
  2. 如果沒有,請檢查php.ini中是否啟用了擴展,然后檢查apache錯誤日志以獲取錯誤消息! dll應該遵循php所使用的相同編譯器。 (VC9或VC6)順便說一句,memcache.dll沒問題

你可以獲得php擴展“memcache”來在windows上使用memcached與php http://downloads.php.net/pierre/

Memcached是服務器守護進程,你可以在這里獲取它的http://splinedancer.com/memcached-win32/

你的composer.json應該在其中列出了ext-memcached ,但它不會安裝,如果它丟失,它只會拋出一個錯誤。 以下是獲取它的各種方法:

Windows二進制路由

截至2018年的AFAIK沒有用於PHP 7的JUST Memcached的二進制Windows端口但是在Laragon或者Winginx中有一個預先打包的版本 在此輸入圖像描述

Windows DLL路由

有一部分人在github上提供編譯的DLL (64位,並提供線程安全)

用於Linux路由的Windows子系統

ubuntu
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt install php-memcached

如果使用它sudo service php7.2-fpm restart重啟php fpm

從源路由編譯

你可以編譯php綁定但是memcachedwindows包已經被打破了4年(截至2018年)

僅本地緩存文件Polyfill路由

這是一個圍繞Memcached的臟包裝程序,名為StaticCache,您可以在夾點中使用來從磁盤讀取/寫入值。 它顯然比memcached慢,所以它只是一個Windows開發的鞋子。 如果您喜歡它,可以將其定義為具有相同名稱的polyfill

function StaticCacheClear()
{
    foreach (scandir(sys_get_temp_dir()) as $file) {
        if (StringBeginsWith($file, "staticcache"))
        {
            $path = sys_get_temp_dir() ."/". $file;
            unlink($path);
        }
    }
    global $Memcache;
    if ($Memcache) $Memcache->flush();
}

// REMOVE if you don't want a global way to clear cache
if (isset($_GET['clear_static_cache'])) {
    StaticCacheClear();
}

function MemcacheGet($key)
{
    global $Memcache;
    $value = $Memcache ? $Memcache->get($key) : (file_exists($key)?file_get_contents($key):null);

    return !$Memcache? $value : (Memcached::RES_NOTFOUND === $Memcache->getResultCode() ? null : $value);
}


function StaticCacheKey($key)
{
    global $Memcache;
    $cacheVersion = "MY_APP_VERSION_HERE";
    $uniqueKey = "staticcache_{$key}_"  . date("Ymd") . "$cacheVersion.cache";
    $filename = sanitize_file_name($uniqueKey);
    $filename = sys_get_temp_dir() . '/' . $filename;
    return $Memcache ? $uniqueKey : $filename;
}

function StaticCacheWrite($key, $value)
{
    global $Memcache;
    if (isset($_GET['disable-cache'])) return null;
    if ($Memcache)
        $Memcache->set(StaticCacheKey($key), serialize($value));
    else
        file_put_contents(StaticCacheKey($key), serialize($value));
}

function StaticCacheRead($key)
{
    global $Memcache;
    $key = StaticCacheKey($key);
    $value = MemcacheGet($key);
    return $value !== null ? unserialize($value) : null;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM