Last Updated:

Creating a Cache Cleanup Extension for OpenCart 3

Hello. We continue to study OpenCart 3, so in the last article "Working with cache in OpenCart 3" we looked at the basic methods of working with cache. But as many have noticed, we don't have a method that removes the entire cache. Let's fix this and implement a method to completely remove the cache.

Just want to warn you that in OpenCart 3 there are several types of cache storage:

  • File
  • Memcache
  • Repeat
  • Mem
  • Apc

The default is file. So I'll write an extension for him.

Changing the File Adapter

Открываем файл system/library/cache/file.php

And add the deleteAll method to it:

public function deleteAll()

{

$files = glob(DIR_CACHE . 'cache.*');

if($files){

foreach ($files as $file) {

if(file_exists($file)){

unlink($file);

}

}

}

}

Now our adapter can delete the entire cache.

Add a method to the cache class to remove the entire cache

Open the system/library/cache file.php and add the deleteAll() method to it:

public function deleteAll()

{

return $this->adaptor->deleteAll();

}

Excellent we have added to our OpenCart new functionality - cache cleaning. Let's create a cache controller in the admin panel in the tool that will delete the entire cache.

Creating a Controller to Delete a Cache in OpenCart

In admin/controller/tool, let's create a cache file.php and put the following code in it:

<?php

class ControllerToolCache extends Controller {

private $error = array();

public function index()

{

if($this->validate()){

$this->clearAll();

}

return $this->response->redirect($this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true));

}

public function clearAll() {

$this->cache->deleteAll();

return $this->response->redirect($this->url->link('common/dashboard','user_token='.$this->session->data['user_token'], true));

}

protected function validate() {

if (!$this->user->hasPermission('modify', 'tool/cache')) {

$this->error['warning'] = 'Ooops';

}

return !$this->error;

}

}

index method – this method will be called by default when we follow the link:

index.php?route=tool/cache

And in the index method, we'll call the validate method, which checks the change access permission. And if access is available, it will call the deleteAll() method, which deletes the cache.

Now, let's create a link on the main page on which the cache will be deleted.

Create a link to delete a cache.

First, open the controller responsible for the main page of the dashboard admin panel: admin/controller/common/dashboard.php and in the index method we will insert:

$data['linkClearCache'] = $this->url->link('tool/cache','user_token=' . $this->session->data['user_token'],true);

We put the url on our cache cleanup controller in the element with the linkClearCache key of the data array.

Now in the template, add a link. To do this, in the file: admin/view/template/common/dashboard.twig add before the button:

<button type="button" id="button-setting" title="{{ button_setting }}" data-loading-text="{{ text_loading }}" class="btn btn-info"><i class="fa fa-cog"></i></button>

Link:

<a href="{{ linkClearCache }}" class="btn btn-success">Clear cache</a>

Great, let's see what we got, go to the admin panel:

Now, as in the previous article "Working with cache in OpenCart 3", we will use the home page controller. And in the index method, the home controller will add:

<?php

class ControllerCommonHome extends Controller {

public function index() {

$this->cache->set('test','test');

var_dump($this->cache->get('test'));die;

Let's see what happened:

OpenCart 3 2

Now let's remove the line:

$this->cache->set('test','test');

And again we will go to the main page of our online store. As you can see, the data is taken from the cache. Now let's go to the admin panel and click "Clear cache" and go to the main page:

OpenCart 3 3

As you can see, the cache has been cleaned.

Conclusion

We have added a new functionality for OpenCart 3 – cache cleaning. Everything is fine, everything works. But there is one caveat: You and I have changed the OpenCart system files, and this cannot be done. And when updating, all the files will return to their original state and our edits will disappear. Therefore, in the next article we will create a modifier that will allow us to use our functionality without touching the system files.

So, we subscribe to a group in contact or Facebook, so as not to miss the release of a new article.