【发布时间】:2015-05-21 16:18:28
【问题描述】:
我目前有 laravel 照明数据库和 eloquent 在 laravel 之外工作。
我现在正在尝试让缓存也能正常工作。
这就是我现在所拥有的。
<?php
require dirname(dirname(__DIR__)) . '/vendor/autoload.php';
require dirname(__DIR__) . '/config.php';
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Cache\CacheManager as CacheManager;
$dbc = new DB;
$dbc->addConnection(array(
'driver' => 'mysql',
'host' => DB_HOST,
'database' => DB_NAME,
'username' => DB_USER,
'password' => DB_PASSWORD,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
));
# Set the default fetch mode for all queries
$dbc->setFetchMode(PDO::FETCH_CLASS);
# Set up the cache
$container = $dbc->getContainer();
$container['config']['cache.driver'] = 'memcached';
$container['config']['cache.memcached'] = array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 100);
$container->offsetGet('config')->offsetSet('cache.driver', 'array');
$cacheManager = new CacheManager($container);
$dbc->setCacheManager($cacheManager);
$dbc->setAsGlobal();
$dbc->bootEloquent();
global $dbc;
这对我不起作用,尽管安装了 memcached 和 php 模块 memcached 并且可以正常工作。
更新我没有得到这个配置的错误。我只是没有看到任何东西进入 memcached。我已经使用以下代码行进行了测试。
$dbc->table('users')->limit(10)->cacheTags(array('people', 'authors'))->remember(10)->get();
一边看盒子一边用
[root@localhost vagrant]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stats items
END
【问题讨论】:
-
哪个版本的 Laravel?
-
我使用的是版本 4。* @JosephSilber
-
仅供参考,DB 包的缓存模块在 Laravel 5 中已经停止使用。所以即使你可以让它工作,我仍然建议你不要使用它。
标签: php laravel memcached illuminate-container