How to disable cache in open cart CMS - php

I used to set $expire = 0; in all cache.php files. Delete all from cache folder. Put $this->cache->delete(); in some random files. Use Ctrl+F5 in my brouser. But cache still alive.

Easiest way would be to just return false from the cache->get method:
system/library/cache.php:
public function get($key) {
return false;

Open up system/library/cache.php and comment these:
public function get($key) {
// return $this->adaptor->get($key);
}
public function set($key, $value) {
// return $this->adaptor->set($key, $value);
}
I know, it's very annoying. OpenCart really should implement an easier way to disable cache.

template_cache takes the value from developer_theme. Do the following:
Go to oc_setting table (database)
Find (developer_theme) of the key column
Then change the value column to 0

I found it!! This worked for me... find vqmod.php file and before $this->_filesModded[$sourcePath] = array('cached' => $changed); Assign $changed to false like $changed = false; save and reload page.

Related

Use SymfonyContainer in a decorated service

I'm developing a module for PS1.7.8.6 which add datas to a stock movement. In order to do that, I have decorate the PrestaShop\PrestaShop\Core\Stock\StockManager following the doc.
I don't know if the decoration was made in a good way because the stock movement in the BO still use PrestaShop\PrestaShop\Core\Stock\StockManager instead of my class CustomStockManager.
But in my module, if I call my CustomStockManager, the movement is not made. I've found that the function SymfonyContainer::getInstance(); return null so the function saveMovement() return false.
Is there a way to know why the SymfonyContainer return null ?
Thanks
Finally I have found a solution. The function SymfonyContainer::getInstance() return null because the function is called from an ajax.php file which call a function from the module.
To resolve it, first, I have put the function getKernel() from this article in the file mymodule.php.
Then, il my CustomStockManager, in the function saveMovement(), I have change this
$sfContainer = SymfonyContainer::getInstance();
if (null === $sfContainer) {
return false;
}
To
$sfContainer = SymfonyContainer::getInstance();
if (null === $sfContainer) {
$moduleObj = Module::getInstanceByName('mymodule');
$sfContainer = $moduleObj->getKernel()->getContainer();
}
And because there is no context and so no user to to the movement, at the beginning of the function I add :
if (is_null(Context::getContext()->employee)) {
$context = Context::getContext();
$context->employee = new Employee(1);
}
It works now but I think this is not the best way to do that. So I will change my ajax.php file in a controller for the module.

Codeigniter 4 /CI4/ Using database to keep site configurations/ How to make key and value accessible globally

I have a database that holds several basis configuration settings for my site. The idea is to pull the values from the database and then make the values accessible globally. I also would like to implement $this->cachePage(DAY); to make sure the site is not constantly asking for configurations settings.
This is mysql table
I have put the following code into public function initController method of the
BaseController:
$global_app_settings = array();
$this->AppSettingModel = new \App\Models\AppSettingModel;
$app_settings = $this->AppSettingModel->getAppSettings();
foreach ($app_settings as $row) {
$global_app_settings[$row->app_setting_key] = $row->app_setting_value;
}
The above code works fine. If I print to screen, it produces the following;
Array
(
[dh_company_name] => A Big Company
[dh_phone_number] => +81-3-5555-5555
[dh_fax_number] => +81-3-5555-5556
)
My questions are as follows;
how can I make the array viable globally in my app?
Where can I add this? $this->cachePage(DAY);
It's simple actually.
public function loadGlobalSettings()
{
$globalSettings = cache('global_settings');
if (null === $globalSettings) {
$appSettingModel = model('\App\Models\AppSettingModel');
$app_settings = $appSettingModel->getAppSettings();
$globalSettings = [];
foreach ($app_settings as $row) {
$globalSettings[$row->app_setting_key] = $row->app_setting_value;
}
cache()->save('global_settings', $globalSettings, DAY*15);
}
return $globalSettings;
}
How it runs:
Try to get from cache.
If cache not exists (returned null), load model, get settings, then cache it.
Return settings array.
Have fun!
Define a protected variable in BaseController class and set it in initController
Put this in your controller
public function __construct()
{
this->cachePage(15);
}

Laravel 4 clear all expired cache

In Laravel, we can store cache with this:
Cache::put($dynamickey, 'value', $minutes);
But this will end up more and more cache files stored even after it is expired. If we try to clean it with php artisan cache:clear or Cache::flush();, it will wipe out all the cache including those that are still valid.
Is it possible to have auto clean up that will clear only expired cache? Thanks.
$value = Cache::remember('users', function()
{
return DB::table('users')->get();
});
Does the work. It validate if cache with given key exists and returns its value. It it does not exist or expired then refresh given cache key with new value.
For Images cache I uses logic like:
tore image md5($file); //where $file === full image path with
image name
Store image md5(file_get_contents($file)); //self explaining method :)
Then
if (Cache::has($cacheKey_name) && !Cache::has($cacheKey_content))
{
Cache::forget($cacheKey_name);
Cache::forget($cacheKey_content);
}
It will check if image is cached and only content changed. If yes then remove old cache and cache new image (with new content). With this logic you will have always fresh image content (with overwritten images).
Or, you can always create artisan task and create Controller to check all cache data in storage directory, then create Cron Task.
you can create an function like this
function cache($key,$value,$min){
(Cache::has($key))?Cache::put($key,$value,$min):Cache::add($key,$value,$min);
if(Cache::has('caches')){
$cache=Cache::get('caches');
$cache[time()+(60*$min)]=$key;
Cache::forget('caches');
Cache::rememberForever('caches',function() use($cache){
return $cache;
});
}else{
$cache[time()+(60*$min)]=$key;
Cache::rememberForever('caches',function() use($cache){
return $cache;
});
}
$cache=Cache::get('caches');
foreach($cache as $key=>$value)
{
if($key<time())
{
Cache::forget($value);
array_forget($cache, $key);
}
}
Cache::forget('caches');
Cache::rememberForever('caches',function() use($cache){
return $cache;
});}
and to remove this cache empty folders you can edit
vendor\laravel\framework\src\Illuminate\Cache\FileStore.php
on line 182
after this code
public function forget($key)
{
$file = $this->path($key);
if ($this->files->exists($file))
{
$this->files->delete($file);
add a function to remove all empty folders , like blow code
public function forget($key)
{
$file = $this->path($key);
if ($this->files->exists($file))
{
$this->files->delete($file);
RemoveEmptySubFolders($this->getDirectory());
to use this function you can see it
Remove empty subfolders with PHP

PHP APC problems when storing objects

I am trying to build an configuration parser for my application I installed APC today, but everytime I try to put an serialized object in the store, it does not get in there and does not. (I am checking with apc.php for my version[3.1.8-dev] on PHP 5.3.16 [My Dev Environment], so I am sure that the data is not in the cache). this is how I pass the data to the cacher:
// The data before the caching
array (
'key' => md5($this->filename),
'value' => serialize($this->cfg)
);
// The caching interface
function($argc){
$key = $argc['key'];
Cache\APC::getInstance()->set($key,$argc['value']);
}
// The caching method described above
public function set($key, $val) {
if (apc_exists($key)) {
apc_delete ($key);
return apc_store($key, $val);
}
else
return false;
}
// the constructor of the configuration class.
// It 1st looks for the configuration in
// the cache if it is not present performs the reading from the file.
public function __construct($filename = '/application/config/application.ini',
$type = self::CONFIG_INI)
{
if (defined('SYSTEM_CACHE') && SYSTEM_CACHE === 'APC'){
$key = md5($filename);
$cfg = APC::getInstance()->get($key);
if (!empty($cfg)) {
print "From Cache";
$this->cfg = unserialize($cfg);
return;
} else {
print "From File";
}
}
}
I did a few tests and there is not a problem with the MD5() key (which I thought while writing this question) nor with APC itself. I am really stuck on this one, nothing odd in the logs, so if anyone can give me at least some directions will be very appreciated.
Thanks in advance!
The problem is was in my code:\
public function set($key, $val) {
/*
*
* If the key exists in the cache delete it and store it again,
* but how could it exist when the else clause is like that...
*/
if (apc_exists($key)) {
apc_delete ($key);
return apc_store($key, $val);
}
// This is very wrong in the current case
// cuz the function will never store and the if will
// never match..
else
return false;
}
NOTE:
Always think and keep your eyes open, if you still can't find anything get off the PC and give yourself a rest. Get back after 10-15 minutes and pown the code. It helps! :D

Zend Cache Howto PHP

How can i do the followin tasks
public function addToCache($id,$items)
{
// in zend cache
}
public function getFromCache($id)
{
// in zend cache
}
The first method should take an id and items which should be cached.
The second method should just take an id of an cached object, and should return the content of the cache of that item id.
i want to be able to do something like that;
public function getItems()
{
if(!$this->cache->getFromCache('items'))
{
$this->addToCache('items',$this->feeds->items());
return $this->cache->getFromCache('items');
}
}
How can i do the both methods in zend cache ?
Everything to get started is in the Zend docs. You do have to dig in a little and get comfortable, it's not a "quick how do I do this" type of area.
But, the general cache-check looks like this:
$cache = /*cache object*/
if ( !($my_object = unserialize($cache->load('cache_key'))) ) {
$my_object = /*not found so initialize your object*/
$cache->save(serialize($my_object)); // magically remembers the above 'cache_key'
}
$my_object->carryOnAsIfNothingStrangeJustHappenedThere();
Assuming that you have already set up an instance of Zend_Cache and have access to it through a local variable $this->cache, your functions would be implemented as:
function getFromCache($key) { return $this->cache->load($key); }
function addToCache($key,$value) { $this->cache->save($key,$value); }

Categories