Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

laravel 5 windows下支持memcache #38

Open
clms2 opened this issue Mar 13, 2019 · 0 comments
Open

laravel 5 windows下支持memcache #38

clms2 opened this issue Mar 13, 2019 · 0 comments

Comments

@clms2
Copy link
Owner

clms2 commented Mar 13, 2019

php_memcached.dll扩展在windows下暂时找不到安装方式,原因

所以laravel下使用memcache扩展并修改相关的系统文件。原文地址, 防止以后访问不了,这边也记录下。
打包下载
手工修改:

  • vendor/laravel/framework/src/Illuminate/Session/SessionManager.php,并增加如下代码:
/**
     * Create an instance of the Memcached session driver.
     *
     * @return IlluminateSessionStore
     */
    protected function createMemcacheDriver()
    {
        return $this->createCacheBased('memcache');
    }
  • vendor/laravel/framework/src/Illuminate/Cache/CacheManager.php 增加以下代码:
/**
     * Create an instance of the Memcache cache driver.
     *
     * @return IlluminateCacheMemcachedStore
     */
    protected function createMemcacheDriver(array $config)
    {
        $prefix = $this->getPrefix($config);

        $memcache = $this->app['memcache.connector']->connect($config['servers']);

        return $this->repository(new MemcacheStore($memcache, $prefix));
    }
  • /vendor/laravel/framework/src/Illuminate/Cache/CacheServiceProvider.php 修改代码:
/**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('cache', function ($app) {
            return new CacheManager($app);
        });

        $this->app->singleton('cache.store', function ($app) {
            return $app['cache']->driver();
        });

        $this->app->singleton('memcached.connector', function () {
            return new MemcachedConnector;
        });

        $this->app->singleton('memcache.connector', function () {
            return new MemcacheConnector;
        });

        $this->registerCommands();
    }

/**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return [
            'cache', 'cache.store', 'memcached.connector', 'command.cache.clear', 'command.cache.table', 'memcache.connector',
        ];
    }
  • 新增文件 /vendor/laravel/framework/src/Illuminate/Cache/MemcacheConnector.php:
<?php
namespace Illuminate\Cache;

use Memcache;
use RuntimeException;

class MemcacheConnector {

    /**
     * Create a new Memcached connection.
     *
     * @param array  $servers
     * @return Memcache
     */
    public function connect(array $servers)
    {
        $memcache = $this->getMemcache();

        // For each server in the array, we'll just extract the configuration and add
        // the server to the Memcache connection. Once we have added all of these
        // servers we'll verify the connection is successful and return it back.
        foreach ($servers as $server)
        {
            $memcache->addServer($server['host'], $server['port'], $server['weight']);
        }

        if ($memcache->getVersion() === false)
        {
            throw new RuntimeException("Could not establish Memcache connection.");
        }

        return $memcache;
    }

    /**
     * Get a new Memcache instance.
     *
     * @return Memcached
     */
    protected function getMemcache()
    {
        return new Memcache;
    }
}
  • 新增文件 /vendor/laravel/framework/src/Illuminate/Cache/MemcacheStore.php:
<?php

namespace Illuminate\Cache;

use Memcache;
use Illuminate\Contracts\Cache\Store;

class MemcacheStore extends TaggableStore implements Store
{
    /**
     * The Memcached instance.
     *
     * @var \Memcached
     */
    protected $memcached;

    /**
     * A string that should be prepended to keys.
     *
     * @var string
     */
    protected $prefix;

    public function __construct(Memcache $memcache, $prefix = '')
    {
        $this->memcache = $memcache;
        $this->prefix = strlen($prefix) > 0 ? $prefix.':' : '';
    }

    /**
     * Retrieve an item from the cache by key.
     *
     * @param  string|array  $key
     * @return mixed
     */
    public function get($key)
    {
        return $this->memcache->get($this->prefix.$key);
    }


    /**
     * Retrieve multiple items from the cache by key.
     *
     * Items not found in the cache will have a null value.
     *
     * @param  array  $keys
     * @return array
     */
    public function many(array $keys)
    {
        $prefixedKeys = array_map(function ($key) {
            return $this->prefix.$key;
        }, $keys);

        $values = $this->memcache->getMulti($prefixedKeys, null, Memcache::GET_PRESERVE_ORDER);

        if ($this->memcache->getResultCode() != 0) {
            return array_fill_keys($keys, null);
        }

        return array_combine($keys, $values);
    }

    /**
     * Store an item in the cache for a given number of minutes.
     *
     * @param  string  $key
     * @param  mixed   $value
     * @param  int     $minutes
     * @return void
     */
    public function put($key, $value, $minutes)
    {
        $compress = is_bool($value) || is_int($value) || is_float($value) ? false : MEMCACHE_COMPRESSED;
        $this->memcache->set($this->prefix.$key, $value, $compress, $minutes * 60);
    }

    /**
     * Store multiple items in the cache for a given number of minutes.
     *
     * @param  array  $values
     * @param  int  $minutes
     * @return void
     */
    public function putMany(array $values, $minutes)
    {
        $prefixedValues = [];

        foreach ($values as $key => $value) {
            $prefixedValues[$this->prefix.$key] = $value;
        }

        $this->memcache->setMulti($prefixedValues, $minutes * 60);
    }
     /**
     * Store an item in the cache if the key doesn't exist.
     *
     * @param  string  $key
     * @param  mixed   $value
     * @param  int     $minutes
     * @return bool
     */
    public function add($key, $value, $minutes)
    {
        return $this->memcache->add($this->prefix.$key, $value, $minutes * 60);
    }

    /**
     * Increment the value of an item in the cache.
     *
     * @param  string  $key
     * @param  mixed   $value
     * @return int|bool
     */
    public function increment($key, $value = 1)
    {
        return $this->memcache->increment($this->prefix.$key, $value);
    }

    /**
     * Decrement the value of an item in the cache.
     *
     * @param  string  $key
     * @param  mixed   $value
     * @return int|bool
     */
    public function decrement($key, $value = 1)
    {
        return $this->memcache->decrement($this->prefix.$key, $value);
    }

    /**
     * Store an item in the cache indefinitely.
     *
     * @param  string  $key
     * @param  mixed   $value
     * @return void
     */
    public function forever($key, $value)
    {
        $this->put($key, $value, 0);
    }

    /**
     * Remove an item from the cache.
     *
     * @param  string  $key
     * @return bool
     */
    public function forget($key)
    {
        return $this->memcache->delete($this->prefix.$key);
    }

    /**
     * Remove all items from the cache.
     *
     * @return void
     */
    public function flush()
    {
        $this->memcache->flush();
    }

    /**
     * Get the underlying Memcached connection.
     *
     * @return \Memcached
     */
    public function getMemcached()
    {
        return $this->memcache;
    }

    /**
     * Get the cache key prefix.
     *
     * @return string
     */
    public function getPrefix()
    {
        return $this->prefix;
    }

    /**
     * Set the cache key prefix.
     *
     * @param  string  $prefix
     * @return void
     */
    public function setPrefix($prefix)
    {
        $this->prefix = ! empty($prefix) ? $prefix.':' : '';
    }
}
  • 修改config/cache.php 新增代码:
'memcache' => [
            'driver'  => 'memcache',
            'servers' => [
                [
                    'host' => env('MEMCACHED_HOST', '127.0.0.1'),
                    'port' => env('MEMCACHED_PORT', 11211),
                    'weight' => 100,
                ],
            ],
        ],
  • 最后,再需要用到memcache的地方把driver修改即可,eg:'driver' => 'memcache',
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant