Flushing CodeIgniter's unused or timeout cache - codeigniter

How can find and deleter unused cache file in Codeigniter 2.x version.
Thanks in advance.

Using $this->cache->cache_info(); fetch all multidimensional array saved cached data and delete cache using this method $this->cache->delete();
Delete Cache
$all_cache = $this->cache->cache_info();
foreach ($all_cache as $cache_id => $cache) :
$this->cache->delete($cache_id);
endforeach;

Related

how update cached data when main data is change in laravel

i want to use a cache of laravel for ex: index method on a specific controllers!
i use Cache::rememberForever method of laravel cache.
i dont use Cache::remember with ttl time for caching data!
my question: i dont no when and how i update data in cache
imaging: i cached user profile with all relations! now user change avatar or personal data! now i should be renew (update) cache data in redis! (update cache data for get in next call) i want to know the best solution for updating cache data when update main data
To update a cache you can use such. event function in your User model:
protected static function boot()
{
parent::boot();
$removeCacheFunc = function ($model) {
$key = self::USER_CACHE_KEY . $model->id; //compile cache key in your way
\Cache::delete($key);
};
static::saved($removeCacheFunc);
static::deleting($removeCacheFunc);
}
Next time you call Cache::rememberForever() will not find this entity by key and will make it on the fly

Why is Laravel's database cache driver not remembering the items I tell it to?

I am implementing relatively simple caching (database driver) on Laravel 8 and have created the cache table in the database using the suggested migration :
$table->string('key')->unique();
$table->text('value');
$table->integer('expiration');
When I use a controller to store a simple entry in the cache it works as expected :
Cache::put('giles', "SOMETHING", 1000);
I can see the entry in the cache table.
But storing more complicated things isn't having the expected result. My original code is :
$statistics = Statistics::all();
$emails = OutgoingEmail::orderBy('created_at', 'DESC')->take(10)->get();
return view('admin.home')->with(compact('emails', 'statistics'));
Whether I try to use the remember method :
$expire = 1000;
// also tried $expire = Carbon::now()->addMinutes(10);
$statistics = Cache::remember('statistics', $expire, function() {
return Statistics::all();
});
or try a more inelegant method of seeing whether the caching is set first, retrieving it if so, or retrieving the collection then using Cache::set() (code not show)...it's not storing it in the cache table.

RuntimeException laravel 5.8 RuntimeException This driver does not support creating temporary URLs

RuntimeException This driver does not support creating temporary URLs.
I am trying to generating Temp Url for every request Laravel version 5.8 below code trying showing an error.
This driver does not support creating temporary URLs.
$url = "66.jpeg";
$url = Storage::disk('public')->url($url);
$url = Storage::disk('public')->temporaryUrl(
'66.jpeg', now()->addMinutes(5)
);
From my knowledge, temporaryUrl is a method used on a drivers such as s3 to create a temporary url for a privately stored asset.
If you would like to set a temporary url for a file, it may help to use Cache to temporarily store the path.
Cache can set a key/value for a set amount of time. A url can be create which links to an endpoint. Then endpoint can then be created which returns the contents of that file:
// Creating temp file index in cache
$image = '66.jpg';
Cache::put('/temp/' . $image, 300); // 5 minutes
Now in, for example, TempController.php (visiting http://example.com/temp/66.jpg):
public function show($image)
{
if (Cache::get('/temp/' . $image) && ! Storage::disk('public')->exists($image)) {
// not in cache or do not exist, maybe redirect...
};
return Storage::disk('public')->get($image);
}
This is a proof of concept however I hope this helps.

Laravel5.5: save Provider data in session to avoid overload

I have made a ServiceProvider to load data on several views. Like this:
View::composer(['components.navigation.main.search','search.*','page-parts.cats','page-parts.categories_menu','page-parts.categories_more','page-parts.cats_top','components.modals.off-category'],function ($view) {
$view->with([
'toplevel_categories' => Category::topLevel()->orderBy('name')->get(),
]);
});
But on several html pages he needs to load multiple of these views and I don't want to load the topLevel categories each time to avoid overload and less runtime.
Can I store the loaded data (toplevel_categories) in a session or what is the most efficient way to handle this problem?
You could simply cache the variable and use it in the callback like:
$topLevelCategories = Category::topLevel()->orderBy('name')->get();
View::composer([], function($view) use ($topLevelCategories) {
$view->with([
'toplevel_categories' => $topLevelCategories
}
You could even use the cache mechanic from laravel itself to save an additional query, like caching it for 30 minutes (assuming the database hasnt changed in the meantime):
// Save the categories in the cache or retrieve them from it.
$topLevelCategories = Cache::remember('topLevelCategories', 30, function() {
return Category::topLevel()->orderBy('name')->get();
});
Note that for Laravel 5.8 the second parameter is in SECONDS, for 5.7 and below it is in MINUTES.
Since your service provider is only loaded once per request/lifecycle this should do the trick.

getting random testimonial in magento not working due to cache

I am trying to display random testimonials, but due to magento cache the random is not working, i have to flush the cache each time to see the testimonials change, my code
public function getTestimonialsLast(){
$collection = Mage::getModel('testimonial/testimonial')->getCollection();
$collection->getSelect()->order(new Zend_Db_Expr('RAND()'));
$collection->addFieldToFilter('status',1);
$collection->setPageSize(5);
return $collection;
}
how can i make it work , how can i make it so that whenever the page is refreshed the collection is randomized.
Any help is greatly appreciated.
Thank you in advance,
One possibility is in the view file:
You can stop Magento from caching the block by adding a false parameter when you implement the block.
<?php echo $this->getChildHtml('testimonials', false) ?>
Because of
Method Summary
string getChildHtml ([string $name = ‘’], [boolean $useCache = true], [ $sorted = true])
Or you could add the cache lifetime to your testimonial class:
public function getCacheLifetime() { return null; }
Are you caching the block within the modules public function __construct()
It would have information on 'cache_lifetime'
Removing the cache block would prevent it from being cached and perform a fresh call each time.

Resources