infusionweb/laravel-remote-content-cache

提供用于从远程服务器获取内容的Laravel缓存包。

1.3.1 2022-07-19 02:09 UTC

README

Latest Stable Version Total Downloads Latest Unstable Version License

一个用于从远程服务器检索和缓存内容的Laravel包。

此包提供了一种方便的方式从远程服务器检索内容并在Laravel中进行缓存。

假设这个过程将用于创建无头CMS设置的Laravel前端。然而,它当然还有其他同样适用的情况。

配置完全由一个单独的Laravel配置文件处理。

安装

步骤 1: Composer

通过Composer命令行

$ composer require infusionweb/laravel-remote-content-cache

或将包添加到您的 composer.json

{
    "require": {
        "infusionweb/laravel-remote-content-cache": "~1.3.1"
    }
}

步骤 2: 注册服务提供者

将服务提供者添加到您的 config/app.php

'providers' => [
    //
    Kozz\Laravel\Providers\Guzzle::class,
    InfusionWeb\Laravel\ContentCache\ContentCacheServiceProvider::class,
];

步骤 3: 启用外观

将外观添加到您的 config/app.php

'aliases' => [
    //
    'Guzzle' => Kozz\Laravel\Facades\Guzzle::class,
    'ContentCache' => InfusionWeb\Laravel\ContentCache\ContentCacheFacade::class,
];

步骤 4: 发布包配置文件

$ php artisan vendor:publish --provider="InfusionWeb\Laravel\ContentCache\ContentCacheServiceProvider"

现在,您可以通过编辑 config/contentcache.php 文件来配置远程API端点和其他偏好设置。

步骤 5: 启用Artisan控制台命令

将命令添加到您的 app/Console/Kernel.php

protected $commands = [
    //
    \InfusionWeb\Laravel\ContentCache\ContentCacheCommand::class,
];

这启用了控制台命令,可以用于定期在Laravel中缓存远程内容

$ php artisan content:cache

前面的命令将缓存所有配置的内容类型,而下面的命令将只缓存 "podcasts" 类型(假设它已在 contentcache.php 中定义)。

$ php artisan content:cache podcasts

用法示例

假设在 config/contentcache.php 文件中定义了一个内容类型 "podcasts"

<?php

return [

    /*
     * Default configuration.
     */
    'default' => [
        // Default length of time (in minutes) to cache content.
        'minutes' => 60,
    ],

     /*
      * Configuration for custom content filters.
      */
    'podcasts' => [

        // Length of time (in minutes) to cache content.
        'minutes' => 60 * 3, // 3 hours

        // REST API endpoint for service from which to retrieve content.
        'endpoint' => 'https://podcasts.example.com/api/v1/content/podcasts',
        'query' => ['_format' => 'json'],

        // Perform data filter (value) on given field name (key). So in this
        // case, "id" and "episode" will be cast as integers, and "date_created"
        // and "date_changed" will be cast as Carbon date objects. All other
        // values will be cast as strings.
        'filters' => [
            'id' => 'int',
            'date_created' => 'date',
            'date_changed' => 'date',
            'episode' => 'int',
        ],

        // New fields to be created on cached content object from given field names.
        // E.g. Given an "episode" value of 13 and a "title" of "Lucky 13", the
        // new "slug" attribute (useful for use in routes) will have a value of
        // "13-lucky-13".
        'fields' => [
            'slug' => ['episode', 'title'],
        ],

        // Keys by which the cache should be indexed. I.e. each content
        // object will be cached under each of these index keys.
        'keys' => [
            'id',
            'slug',
            'uuid',
            'episode',
        ],
    ],
];

以下代码可用于缓存和检索播客内容

<?php

use ContentCache;

// Manually cache remote podcast episodes, if this process is not handled via the
// Artisan console command.
ContentCache::profile('podcasts')->cache();

// Then retrieve a list of all episodes.
$episodes = ContentCache::profile('podcasts')->getAll();

// This could also be shortened to:
$episodes = ContentCache::profile('podcasts')->cache()->getAll();

// Individual content objects can be retrieved via configured indexes.
// In this case, by ID.
$episode = ContentCache::profile('podcasts')->getBy('id', 13);

// Or by slug (from a Laravel Request object), using the "magic function".
$cache = ContentCache::profile('podcasts');
$episode = $cache->getBySlug('13-lucky-13');

// This makes it easy to hand off to a Blade template.
return view('pages.podcast.episode', compact('episode'));

致谢

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件