lovecoding / content-cache

此软件包已被废弃,不再维护。未建议替代软件包。

一个简单的软件包——由SLim框架创建的Web服务,帮助您在服务器上缓存内容。一次处理,多次使用。

v2.0.5 2019-02-11 14:34 UTC

This package is auto-updated.

Last update: 2024-05-12 03:38:32 UTC


README

ContentCache是一个简单的软件包——由SLim框架创建的Web服务,帮助您在服务器上缓存内容。一次处理,多次使用。

安装

建议使用Composer安装ContentCache。

$ composer require lovecoding/content-cache

这将安装ContentCache及其所有必需的依赖项。ContentCache需要PHP 5.3.0或更高版本。

使用方法

创建一个index.php文件,内容如下

使用Slim 3.x框架的示例

<?php

require 'vendor/autoload.php';

$app = new Slim\App();

$container = $app->getContainer();

$container['cacheService'] = function() {
    // path to folder contains cached
    $cacheProvider = new \LoveCoding\ContentCache\CacheProvider('storage/cache');
    
    return $cacheProvider;
};

$app->get('/cache/array', function ($request, $response, $args) use($container) {
    $cacheService = $container->get('cacheService');

    // $cacheService->cache return a json
    $contentArrayCache = $cacheService->cacheArray($request, function() {

        // This function will run when $content is null on server
        // TODO something

        // Must return an array
        return ...;
    });
    
    return $response->withJson(json_encode($contentArrayCache));
});

$app->get('/cache/plaintext', function ($request, $response, $args) use($container) {
    $cacheService = $container->get('cacheService');

    $contentCache = $cacheService->cache($request, function() {
        // This function will run when $content is null on server
        // TODO something

        // return and save something you want on server
        return ...;
    });

    return $response->getBody()->write($contentCache);
});

// Using salt for many content
$app->get('/cache/plaintext', function ($request, $response, $args) use($container) {
    $cacheService = $container->get('cacheService');

    $firstList = $cacheService->salt('salt_for_firstList')->cacheArray($request, function() {
        // This function will run when $content is null on server
        // TODO something

        // return and save something you want on server
        return ...;
    });

    $secondList = $cacheService->salt('salt_for_secondList')->cacheArray($request, function() {
        // This function will run when $content is null on server
        // TODO something

        // return and save something you want on server
        return ...;
    });

    return $response->withJson(json_encode([$firstList, $secondList]));
});

$app->run();

您可以使用内置的PHP服务器快速测试此示例

$ php -S localhost:8000

访问https://:8000/cache/arrayhttps://:8000/cache/plaintext将显示您缓存的内容。