1. Go to this page and download the library: Download square/ttcache library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
square / ttcache example snippets
$this->app->singleton(TTCache::class);
class ProductController
{
public function show($productId)
{
$p = Product::find($productId);
$stores = Store::all();
return [
'id' => $p->id,
'name' => $p->name,
'inventory' => $this->inventory($p, $stores),
'price' => $this->price($p)
]
}
public function inventory($product, $stores)
{
foreach ($stores as $store) {
yield $store->id => $this->singleStoreInventory($product, $store);
}
}
public function singleStoreInventory($product, $store)
{
// expensive computation
}
public function price($product)
{
// expensive computation
}
}
public function show($productId)
{
$cacheResult = $this->ttCache->remember('cachekey', tags: [], fn () => 'computed value');
if ($cacheResult->hasError()) {
\Log::error('caching error', ['error' => $cacheResult->error()]);
}
}
$collection = BlogPosts::all();
$t->remember('full-collection', 0, [], function () use ($collection) {
$posts = [];
$keys = [];
// Create an array all the caching keys for items in the collection
// This is actually a map of `entity_id` => `cache_key`
foreach ($collection as $post) {
$keys[$post->id] = __CLASS__.':blog-collection:'.$post->id;
}
// Pre-load them into memory
$this->tt->load($keys);
// Run through the collection as usual, making calls to `->remember` that will either resolve in memory
// Or will set a new value in cache for those items that couldn't be resolved in memory
foreach ($collection as $post) {
$key = __CLASS__.':blog-collection:'.$post->id;
$posts[] = $this->tt->remember($key, 0, ['post:'.$post->id], fn () => "<h1>$post->title</h1><hr /><div>$post->content</div>")->value();
}
return $posts;
})->value();
$postIds = [1, 2, 3, 4, 5, 6];
$t->remember('full-collection', 0, [], function () use ($postIds) {
$posts = [];
$keys = [];
// Create an array all the caching keys for items in the collection
// This is actually a map of `entity_id` => `cache_key`
foreach ($postIds as $postId) {
$keys[$postId] = __CLASS__.':blog-collection:'.$postId;
}
// Pre-load them into memory
$loadResult = $this->tt->load($keys);
// Since we passed our keys to `load` as a map of `entity_id` => `cache_key`, we can retrieve the entity ids here.
$missing = BlogPosts::whereIn('id', array_keys($loadResult->missingKeys()));
// Run through the collection as usual, making calls to `->remember` that will either resolve in memory
// Or will set a new value in cache for those items that couldn't be resolved in memory
foreach ($postIds as $postId) {
$key = __CLASS__.':blog-collection:'.$post->id;
$posts[] = $this->tt->remember($key, 0, ['post:'.$post->id], fn () => "<h1>{$missing[$postId]->title}</h1><hr /><div>{$missing[$postId]->content}</div>")->value();
}
return $posts;
})->value();
class CachingMiddleware
{
public function handle($request, $next)
{
$url = $request->url();
$cachekey = sha1($url);
return $this->ttCache->remember($cachekey, function () use ($next, $request) {
$response = $next($request);
if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
return $response;
}
return new BypassCache($response);
});
}
}
$tt->remember('key:level:1', 0, ['account:123'], function () {
//... code ...
$tt->remember('key:level:2', 0, ['account:123'], function () {
// ... more ... code ...
$tt->remember('key:level:3', 0, ['account:123'], function () {
// ... even ... more ... code ...
$tt->remember('key:level:4', 0, ['account:123'], function () {
// ... wow ... that's ... a ... lot ... of ... code ...
});
});
});
});
$tt->remember('key:level:1', 0, [new HeritableTag('account:123')], function () {
//... code ...
$tt->remember('key:level:2', 0, [], function () {
// ... more ... code ...
$tt->remember('key:level:3', 0, [], function () {
// ... even ... more ... code ...
$tt->remember('key:level:4', 0, [], function () {
// ... wow ... that's ... a ... lot ... of ... code ...
});
});
});
});