php+ Laravel | guzzlehttp / guzzle http客户端缓存无效 /刷新

发布于 2025-02-12 01:15:51 字数 2552 浏览 2 评论 0 原文

问题

我如何通过编程方式告诉guzzle客户端删除/消除或刷新缓存文件夹?

甚至更好,以定位特定文件夹 x y < /strong>?

原因并不重要,但是我可以举个例子,以更好地理解。 假设我们有一本书API(这是我的),我们将其称为Guzzle Client,并具有1个小时的缓存来获得一些书籍价格。该缓存位于文件夹

框架/缓存/数据/data/guzzlefilecache/public/bumart/books/Prices

curl -X GET https://example.com/books/prices 

现在,有人更新了书籍价格。 如果我的主要服务调用书籍/Orices 现在将具有旧价格,因为1小时没有通过。

因此,我有一个系统,当更新书籍价格时,我可以向我的主要服务开发一个活动,并告诉我想要什么。毫不奇怪,我希望我的主要服务是在 framework/cache/data/guzzlefilecache/public/bublic/books/prices 时,即使被解雇,都可以在 frame/data/guzzlefilecache/public/bumple/books/code中无效。

我该怎么做?

首先,请注意,要说我实际上正在使用 lumen 与Laravel相同的问题是一个问题。

因此,假设我在 framework/cache/data/guzzlefilecache/public/blogs 上,我需要一个特定的文件夹,并且在某些点需要使此缓存无效(无论出于何种原因)。

*我知道您可以手动删除文件夹,因此可能是通过编程方式进行的,但是有更好的方法吗? *

这是我当前的代码 创建/使用cache 使用Guzzle客户端,

$ttl = 3600;
// Create a HandlerStack
$stack = HandlerStack::create();
// Create Folder GuzzleFileCache inside the providen cache folder path
$requestCacheFolderName = 'framework/cache/data';
// Retrieve the bootstrap folder path of your Laravel Project
$cacheFolderPath = 'GuzzleFileCache/public/blogs';

// Instantiate the cache storage: a PSR-6 file system cache with
$cache_storage = new Psr6CacheStorage(
    new FilesystemAdapter(
        $requestCacheFolderName,
        $ttl,
        $cacheFolderPath
    )
);

// Add Cache Method
$stack->push(
    new CacheMiddleware(
        new GreedyCacheStrategy(
            $cache_storage,
            $ttl // the TTL in seconds
        )
    ),
    'greedy-cache'
);

所以一种非常丑陋的方法就是删除该文件夹,这里有一些伪代码,

rmdir('framework/cache/data/GuzzleFileCache/public/blogs');

但我正在寻找

use Illuminate\Support\Facades\Http; // <--- laravel guzzle client 

Http::cacheInvalidate('framework/cache/data/GuzzleFileCache/public/blogs');


依赖

"php": "^8.1.3",
"flipbox/lumen-generator": "^9.1",
"guzzlehttp/guzzle": "^7.4", 
"kevinrob/guzzle-cache-middleware": "^4.0",
"laravel/lumen-framework": "^9.0",
"symfony/cache": "^6.1"

项参考

Question

How can I programmatically tell to a Guzzle Client to delete / eliminate or refresh a Cache folder?

An even better, to target a specific folder X or Y?

The reason is not important, but I can put an example for better understand.
Let's say we have book api (which is mine) an with another service we call it with Guzzle Client and has a cache of 1 hour to get some book prices. The cache is at folder

framework/cache/data/GuzzleFileCache/public/books/prices

curl -X GET https://example.com/books/prices 

Now, someone updates the books prices.
If my main service call books/orices will have now the old prices because 1 hour was not passed.

So I've a system that when a book price is updated I can fire an event to my main service and tell whatever I want. Unsurprisingly what I want my main service to do is to invalidate the cache at framework/cache/data/GuzzleFileCache/public/books/prices when that even if fired.

How can I do that?

Code

First of all, note to say that I'm actually using Lumen but it shouldn't be a problem as works the same as Laravel.

So let's say that I've a specific folder at framework/cache/data/GuzzleFileCache/public/blogs and I need at certain point invalidate this cache (for whatever reason).

*I know you can just delete manually the folder, and therefore probably do it programmatically, but is there a better way? *

This is my current code to create/use the cache with a Guzzle Client

$ttl = 3600;
// Create a HandlerStack
$stack = HandlerStack::create();
// Create Folder GuzzleFileCache inside the providen cache folder path
$requestCacheFolderName = 'framework/cache/data';
// Retrieve the bootstrap folder path of your Laravel Project
$cacheFolderPath = 'GuzzleFileCache/public/blogs';

// Instantiate the cache storage: a PSR-6 file system cache with
$cache_storage = new Psr6CacheStorage(
    new FilesystemAdapter(
        $requestCacheFolderName,
        $ttl,
        $cacheFolderPath
    )
);

// Add Cache Method
$stack->push(
    new CacheMiddleware(
        new GreedyCacheStrategy(
            $cache_storage,
            $ttl // the TTL in seconds
        )
    ),
    'greedy-cache'
);

So one very ugly way would be to just delete that folder, here is some pseudocode

rmdir('framework/cache/data/GuzzleFileCache/public/blogs');

But I'm looking at something like

use Illuminate\Support\Facades\Http; // <--- laravel guzzle client 

Http::cacheInvalidate('framework/cache/data/GuzzleFileCache/public/blogs');


Dependencies

"php": "^8.1.3",
"flipbox/lumen-generator": "^9.1",
"guzzlehttp/guzzle": "^7.4", 
"kevinrob/guzzle-cache-middleware": "^4.0",
"laravel/lumen-framework": "^9.0",
"symfony/cache": "^6.1"

References

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慵挽 2025-02-19 01:15:51

出色地。我无法通过标签使缓存无效,但至少将所有缓存都在一起,这已经是某种程度上了。这里有一些代码是否可以帮助某人。


use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;

// Instantiate the cache storage: a PSR-6 file system cache with
$cache = new TagAwareAdapter(
// Adapter for cached items
    new FilesystemAdapter(
        $requestCacheFolderName,
        1,
        $cacheFolderPath
    )
);
$cache->clear();

Well. I could not invalidate the cache by tags but at least all the cache together, which is already something. Here some code if may help someone.


use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;

// Instantiate the cache storage: a PSR-6 file system cache with
$cache = new TagAwareAdapter(
// Adapter for cached items
    new FilesystemAdapter(
        $requestCacheFolderName,
        1,
        $cacheFolderPath
    )
);
$cache->clear();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文