3

I've currently got laravel illuminate database and eloquent working outside of laravel.

I'm now trying to get the cache working as well.

This is what I have altogether now.

<?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;

This is not working for me, despite having memcached and php module memcached installed and working.

UPDATE I get no errors with this configuration. I am just not seeing anything coming into memcached. I've tested with the following line of code.

$dbc->table('users')->limit(10)->cacheTags(array('people', 'authors'))->remember(10)->get();

Whilst watching on the box with

[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
David
  • 34,836
  • 11
  • 47
  • 77

2 Answers2

2

well,I'v met the same problem, there is a github repository named:Using Laravel's Illuminate Components Independently. That perfectly solved my problem, here is the link: https://github.com/mattstauffer/Torch

Poisoner
  • 51
  • 2
  • 9
1

This may help someone using the caching facade in Laravel 5.8, in my case I am using file caching for a Codeigniter project.

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);
ZGuard
  • 152
  • 1
  • 10