【问题标题】:Laravel cache outside of laravelLaravel 缓存在 laravel 之外
【发布时间】: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


【解决方案1】:

好吧,我也遇到了同样的问题,有一个 github 存储库,名为:Using Laravel's Illuminate Components Independently。 完美解决了我的问题 链接在这里: https://github.com/mattstauffer/Torch

【讨论】:

    【解决方案2】:

    这可能有助于使用 Laravel 5.8 中缓存外观的人,在我的情况下,我正在为 Codeigniter 项目使用文件缓存。

    use \Illuminate\Cache\CacheManager;
    use \Illuminate\Filesystem\Filesystem;
    
    $container = $capsule->getContainer();
    $container['config']['cache.stores.file'] = array(
        'driver' => 'file',
        'path' => \APPPATH . 'cache/eloquent' // use your own cache directory
    );
    $container['config']['cache.default'] = 'file';
    $container['files'] = new Filesystem();
    $cacheManager = new CacheManager( $container ); 
    $app->instance( 'cache', $cacheManager);
    

    【讨论】:

      猜你喜欢
      • 2020-03-12
      • 2020-01-22
      • 2015-12-19
      • 2018-01-30
      • 2022-10-20
      • 1970-01-01
      • 2016-02-24
      • 2020-09-10
      相关资源
      最近更新 更多