从php laravel禁用memcached
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从php laravel禁用memcached相关的知识,希望对你有一定的参考价值。
你好我正在研究在php laravel框架上编写的cms,它有memcached缓存服务。
我需要从虚拟服务器重新定位网站,其中memcached可以运行到新主机,所以我需要禁用/避免内存缓存,因为网站使用memcached并不复杂。我怎样才能避免缓存并更改我的php文件。
这是我正在使用的
的index.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(IlluminateContractsHttpKernel::class);
$response = $kernel->handle(
$request = IlluminateHttpRequest::capture()
);
$response->send();
$kernel->terminate($request, $response);
?>
app.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 ITDCMSSystemFoundationApplication(
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(
IlluminateContractsHttpKernel::class,
ITDCMSSystemAppHttpKernel::class
);
$app->singleton(
IlluminateContractsConsoleKernel::class,
ITDCMSSystemAppConsoleKernel::class
);
$app->singleton(
IlluminateContractsDebugExceptionHandler::class,
ITDCMSSystemAppExceptionsHandler::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;
?>
autoload.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;
}
?>
cache.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 IlluminateCache;
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致命错误:第51行/home/remi/domains/public_html/vendor/laravel/framework/src/Illuminate/Cache/MemcachedConnector.php中找不到类'Memcached'
非常感谢
答案
在.env
文件(项目的根目录)中,设置以下内容:
CACHE_DRIVER=array
这将导致对缓存服务的任何调用都存储在内存中,纯粹是为了调用它的请求。
或者,在您的cache.php
配置文件中,您可以更改
'default' => env('CACHE_DRIVER', 'file'),
至:
'default' => env('CACHE_DRIVER', 'array'),
如果你没有在你的.env中定义CACHE_DRIVER
,并且想要默认使用数组。
以上是关于从php laravel禁用memcached的主要内容,如果未能解决你的问题,请参考以下文章
有没有人让 memcached 在 Laravel Homestead php7 盒子上工作?
有没有人让 memcached 在 Laravel Homestead php7 盒子上工作?
PHPUnit 测试:写入/从 Memcached 检索的问题