代码之家  ›  专栏  ›  技术社区  ›  Irakli Balanchivadze

从php laravel禁用memcached

  •  1
  • Irakli Balanchivadze  · 技术社区  · 6 年前

    您好,我正在开发基于php laravel框架编写的cms和它的hase memcached缓存服务。

    我需要将网站从可以运行memcached的虚拟服务器重新定位到新的主机,所以我需要禁用/避免memcache,因为使用memcached并没有那个么复杂。如何避免缓存和更改php文件。

    以下是我的工作内容

    指数php

     <?php
    
    $_PATH = __DIR__ . '/..';
    
    /*
    |--------------------------------------------------------------------------
    | Register The Auto Loader
    |--------------------------------------------------------------------------
    |
    | Composer provides a convenient, automatically generated class loader for
    | our application. We just need to utilize it! We'll simply require it
    | into the script here so that we don't have to worry about manual
    | loading any of our classes later on. It feels nice to relax.
    |
     */
    require $_PATH . '/core/bootstrap/autoload.php';
    
    /*
    |--------------------------------------------------------------------------
    | Turn On The Lights
    |--------------------------------------------------------------------------
    |
    | We need to make jigar PHP development, so let us turn on the lights.
    | This bootstraps the framework and gets it ready for use, then it
    | will load up this application so that we can run it and send
    | the responses back to the browser and delight our users.
    |
     */
    
    $app = require_once $_PATH . '/core/bootstrap/app.php';
    
    /*
    |--------------------------------------------------------------------------
    | Run The Application
    |--------------------------------------------------------------------------
    |
    | Once we have the application, we can handle the incoming request
    | through the kernel, and send the associated response back to
    | the client's browser allowing them to enjoy the creative
    | and wonderful application we have prepared for them.
    |
     */
    $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
    
    $response = $kernel->handle(
        $request = Illuminate\Http\Request::capture()
    );
    
    $response->send();
    
    $kernel->terminate($request, $response);
    
    ?>
    

    应用程序。php

        <?php
    
    /*
    |--------------------------------------------------------------------------
    | Create The Application
    |--------------------------------------------------------------------------
    |
    | The first thing we will do is create a new CMS application instance
    | which serves as the "glue" for all the components of CMS, and is
    | the IoC container for the system binding all of the various parts.
    |
    */
    
    $app = new ITDCMS\System\Foundation\Application(
        realpath($_PATH)
    );
    
    
    /*
    |--------------------------------------------------------------------------
    | Bind Important Interfaces
    |--------------------------------------------------------------------------
    |
    | Next, we need to bind some important interfaces into the container so
    | we will be able to resolve them when needed. The kernels serve the
    | incoming requests to this application from both the web and CLI.
    |
    */
    $app->singleton(
        Illuminate\Contracts\Http\Kernel::class,
        ITDCMS\System\App\Http\Kernel::class
    );
    
    $app->singleton(
        Illuminate\Contracts\Console\Kernel::class,
        ITDCMS\System\App\Console\Kernel::class
    );
    
    $app->singleton(
        Illuminate\Contracts\Debug\ExceptionHandler::class,
        ITDCMS\System\App\Exceptions\Handler::class
    );
    
    
    /*
    |--------------------------------------------------------------------------
    | Return The Application
    |--------------------------------------------------------------------------
    |
    | This script returns the application instance. The instance is given to
    | the calling script so we can separate the building of the instances
    | from the actual running of the application and sending responses.
    |
    */
    return $app;
    ?>
    

    自动加载。php

      <?php
    
    define('ITDCMS_START', microtime(true));
    
    /*
    |--------------------------------------------------------------------------
    | Register The Composer Auto Loader
    |--------------------------------------------------------------------------
    |
    | Composer provides a convenient, automatically generated class loader
    | for our application. We just need to utilize it! We'll require it
    | into the script here so that we do not have to worry about the
    | loading of any our classes "manually". Feels great to relax.
    |
    */
    
    require $_PATH.'/system/App/helpers.php';
    
    require $_PATH.'/system/App/Common/Autoloader.php';
    
    require $_PATH.'/vendor/autoload.php';
    
    /*
    |--------------------------------------------------------------------------
    | Include The Compiled Class File
    |--------------------------------------------------------------------------
    |
    | To dramatically increase your application's performance, you may use a
    | compiled class file which contains all of the classes commonly used
    | by a request. The Artisan "optimize" is used to create this file.
    |
    */
    
    $compiledPath = __DIR__.'/cache/compiled.php';
    
    if (file_exists($compiledPath)) {
        require $compiledPath;
    }
    ?>
    

    隐藏物php

        <?php
    
    return [
    
        /*
        |--------------------------------------------------------------------------
        | Default Cache Store
        |--------------------------------------------------------------------------
        |
        | This option controls the default cache connection that gets used while
        | using this caching library. This connection is used when another is
        | not explicitly specified when executing a given caching function.
        |
        */
        'default' => env('CACHE_DRIVER', 'file'),
    
        /*
        |--------------------------------------------------------------------------
        | Cache Stores
        |--------------------------------------------------------------------------
        |
        | Here you may define all of the cache "stores" for your application as
        | well as their drivers. You may even define multiple stores for the
        | same cache driver to group types of items stored in your caches.
        |
        */
    
        'stores' => [
    
            'apc' => [
                'driver' => 'apc',
            ],
    
            'array' => [
                'driver' => 'array',
            ],
    
            'database' => [
                'driver' => 'database',
                'table'  => 'cache',
                'connection' => null,
            ],
    
            'file' => [
                'driver' => 'file',
                'path'   => data_path('cache'),
            ],
    
            'memcached' => [
                'driver'  => 'memcached',
                'servers' => [
                    [
                        'host' => env('MEMCACHED_HOST', '127.0.0.1'),
                        'port' => env('MEMCACHED_PORT', 11211),
                        'weight' => 100,
                    ],
                ],
            ],
    
            'redis' => [
                'driver' => 'redis',
                'connection' => 'default',
            ],
    
        ],
    
        /*
        |--------------------------------------------------------------------------
        | Cache Key Prefix
        |--------------------------------------------------------------------------
        |
        | When utilizing a RAM based store such as APC or Memcached, there might
        | be other applications utilizing the same cache. So, we'll specify a
        | value to get prefixed to all our keys so we can avoid collisions.
        |
        */
    
        'prefix' => env('CACHE_PREFIX', 'app_'),
    
    
        'lifetime' => [
            'default' => 120
        ],
    
    ];
    ?>
    

    MemcachedConnector

    <?php
    
    namespace Illuminate\Cache;
    
    use Memcached;
    use RuntimeException;
    
    class MemcachedConnector
    {
        /**
         * Create a new Memcached connection.
         *
         * @param  array  $servers
         * @return \Memcached
         *
         * @throws \RuntimeException
         */
        public function connect(array $servers)
        {
            $memcached = $this->getMemcached();
    
            // For each server in the array, we'll just extract the configuration and add
            // the server to the Memcached connection. Once we have added all of these
            // servers we'll verify the connection is successful and return it back.
            foreach ($servers as $server) {
                $memcached->addServer(
                    $server['host'], $server['port'], $server['weight']
                );
            }
    
            $memcachedStatus = $memcached->getVersion();
    
            if (! is_array($memcachedStatus)) {
                throw new RuntimeException('No Memcached servers added.');
            }
    
            if (in_array('255.255.255', $memcachedStatus) && count(array_unique($memcachedStatus)) === 1) {
                throw new RuntimeException('Could not establish Memcached connection.');
            }
    
            return $memcached;
        }
    
        /**
         * Get a new Memcached instance.
         *
         * @return \Memcached
         */
        protected function getMemcached()
        {
            return new memcached;
        }
    }
    

    我收到PHP致命错误:在/home/remi/domains/public\u html/vendor/laravel/framework/src/illighted/Cache/MemcachedConnector中找不到类“Memcached”。php在线51

    事先非常感谢

    1 回复  |  直到 6 年前
        1
  •  5
  •   Jono20201    6 年前

    在您的 .env 文件(项目的根目录),设置以下内容:

     CACHE_DRIVER=array
    

    这将导致对缓存服务的任何调用都存储在内存中,并且仅针对已被调用的请求。


    或者,在 cache.php 配置文件,您可以更改

    'default' => env('CACHE_DRIVER', 'file'),
    

    收件人:

    'default' => env('CACHE_DRIVER', 'array'),
    

    如果您不定义 CACHE_DRIVER 在您的中。env,并希望默认使用数组。