webman 怎么使用laravel cache?

wanbao520

移植了/congfig/cache.php ;
use Illuminate\Support\Facades\Cache ;

测试后提示:
RuntimeException: A facade root has not been set. in F:\webman\vendor\illuminate\support\Facades\Facade.php:258
Stack trace:
F:\webman\app\controller\Index.php(10): Illuminate\Support\Facades\Facade::__callStatic('add', Array)
F:\webman\vendor\workerman\webman-framework\src\App.php(146): app\controller\Index->index(Object(support\Request))
F:\webman\vendor\workerman\workerman\Connection\TcpConnection.php(637): Webman\App->onMessage(Object(Workerman\Connection\TcpConnection), Object(support\Request))
F:\webman\vendor\workerman\workerman\Events\Select.php(293): Workerman\Connection\TcpConnection->baseRead(Resource id #196)
F:\webman\vendor\workerman\workerman\Worker.php(2430): Workerman\Events\Select->loop()
F:\webman\vendor\workerman\workerman\Worker.php(1417): Workerman\Worker->run()
F:\webman\vendor\workerman\workerman\Worker.php(1360): Workerman\Worker::forkWorkersForWindows()
F:\webman\vendor\workerman\workerman\Worker.php(542): Workerman\Worker::forkWorkers()
F:\webman\start.php(156): Workerman\Worker::runAll()
{main}

4931 3 1
3个回答

wanbao520

主要是想使用文件缓存,这个功能!

  • 暂无评论
nitron

主要使用文件缓存的话,就比较简单,我只告诉你简单实现过程,具体实际需求自己修改代码
composer require illuminate/cache
composer require illuminate/filesystem

<?php

namespace support\bootstrap;
use Webman\Bootstrap;
use Illuminate\Cache\FileStore;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Cache\Repository;

class Cache implements Bootstrap
{

    protected static $_instance = [];

    public static function start($worker) {
        $fileStore = new FileStore(new Filesystem(),runtime_path());
        self::$_instance = new Repository($fileStore);
    }

    public static function __callStatic($name, $arguments)
    {
        static::$_instance->{$name}(... $arguments);
    }
}

然后 config/bootstrap.php内加入一行

return [
    ......
    support\bootstrap\Cache::class,
]

然后重启就可以了

  • wanbao520 2021-06-04

    非常非常感谢!

  • nitron 2021-06-04

    虽然原代码不影响使用,但还是更正一下吧
    严谨一点,把上面16行的self::$_instance 改成 static::$_instance

  • yzh52521 2021-06-04

    如何改成 redis 或者其他 缓存呢? 请赐教

  • nitron 2021-06-04

    @7845:下面回复你了

  • wanbao520 2021-06-04

    @8001:非常感谢

nitron

@yzh52521
还是简单写的,请根据自己的具体业务需求修改,没有跟laravel一样封装多种缓存驱动,

composer require illuminate/cache
composer require predis/predis
<?php

namespace support\bootstrap;

use Webman\Bootstrap;
use Illuminate\Container\Container;
use Illuminate\Redis\RedisManager;
use Illuminate\Cache\CacheManager;

class Cache implements Bootstrap
{

    protected static $_instance = [];

    public static function start($worker) {
        $container = new Container;
        $container['config'] = [
            'cache.default' => 'redis',
            'cache.stores.redis' => [
                'driver' => 'redis',
                'connection' => 'default'
            ],
            'cache.prefix' => 'webman',
            'database.redis' => [
                'cluster' => false,
                'default' => [
                    'host' => '127.0.0.1',
                    'port' => 6379,
                    'database' => 0,
                    'password' => null
                ],
            ]
        ];

        $container['redis'] = new RedisManager($container, 'predis', $container['config']['database.redis']);
        $cache = new CacheManager($container);
        static::$_instance = $cache->store();
    }

    public static function __callStatic($name, $arguments)
    {
        static::$_instance->{$name}(... $arguments);
    }
}
  • yzh52521 2021-06-04

    我按你file改的

    $config = config('redis');
    $reidsStore =new RedisStore(new RedisManager('', 'phpredis', $config));
    static::$_instance = new Repository($fileStore);

  • nitron 2021-06-04

    可用就行

  • Chuckle 2021-09-18

    @7845:您好 根据你这个 为什么redis写入不了呢

  • li3826373 2022-04-06

    很好用

年代过于久远,无法发表回答
×
🔝