Michael Posted January 18, 2016 Report Posted January 18, 2016 Is there a way to use the Blesta's built in cache for plugins? PauloV 1 Quote
Blesta Addons Posted January 19, 2016 Report Posted January 19, 2016 +1 for this question . what i can help from the basic code, , the cache system is already enabled by default in blesta Configure::set("Caching.on", true) . a sample code like this can work , but not tested . $cache = Cache::fetchcache('your_plugin_controller_pdt_file', $this->company_id . DS . 'plugins' . DS ); // if a copy of cache existe if ($cache) { // here you should return the cached file return unserialize(base64_decode($cache)); } // no cache existe , return output and create cached file else { $pdt_file = $this->view->fetch("your_plugin_controller_pdt_file"); // get the pdt file // create cached file if cache system enabled if ( (Configure::get('Caching.on') && is_writable(CACHEDIR)) ) { Cache::writecache('your_plugin_controller_pdt_file', base64_encode(serialize($pdt_file)), strtotime( Configure::get('Blesta.cache_length') ) - time( ), $this->company_id . DS . 'plugins' . DS ); } } ModuleMatic, Michael and PauloV 3 Quote
Michael Posted January 19, 2016 Author Report Posted January 19, 2016 ah, how would we make a folder in the cache though? Quote
Tyson Posted January 19, 2016 Report Posted January 19, 2016 Something like.. // Fetch from cache $cache = Cache::fetchCache('file_name', $this->company_id . DS . 'directory' . DS); if ($cache) { $data = unserialize(base64_decode($cache)); } else { // Fetch the data to store in the cache $data = 'my data'; // Write to cache if (Configure::get('Caching.on') && is_writable(CACHEDIR)) { try { Cache::writeCache( 'file_name', base64_encode(serialize($data)), strtotime(Configure::get('Blesta.cache_length')) - time(), $this->company_id . DS . 'directory' . DS ); } catch (Exception $e) { // Couldn't cache } } } // Use $data Quote
Michael Posted January 19, 2016 Author Report Posted January 19, 2016 Something like.. // Fetch from cache $cache = Cache::fetchCache('file_name', $this->company_id . DS . 'directory' . DS); if ($cache) { $data = unserialize(base64_decode($cache)); } else { // Fetch the data to store in the cache $data = 'my data'; // Write to cache if (Configure::get('Caching.on') && is_writable(CACHEDIR)) { try { Cache::writeCache( 'file_name', base64_encode(serialize($data)), strtotime(Configure::get('Blesta.cache_length')) - time(), $this->company_id . DS . 'directory' . DS ); } catch (Exception $e) { // Couldn't cache } } } // Use $data So we can set the directory to say cms and it will make /cache/1/cms/ and put the files in there mate like you have with nav? Quote
Blesta Addons Posted January 20, 2016 Report Posted January 20, 2016 Normally the plugin should add the folder in installation or update . Like the support manager . @tyson , what if we want to cache pdt file instead of calling it every time ? Your example for caching just a data . Michael 1 Quote
Michael Posted January 20, 2016 Author Report Posted January 20, 2016 Normally the plugin should add the folder in installation or update . Like the support manager . @tyson , what if we want to cache pdt file instead of calling it every time ? Your example for caching just a data . I believe that's all it is mate, in the nav/ cache files there's a big encryption hash. Quote
Tyson Posted January 20, 2016 Report Posted January 20, 2016 Template files can be cached automatically by specifying so, e.g. class MyController extends AppController { public function myPage() { $this->startCaching(Configure::get("Blesta.cache_length")); ... } } Michael and PauloV 2 Quote
Blesta Addons Posted January 20, 2016 Report Posted January 20, 2016 Template files can be cached automatically by specifying so, e.g. class MyController extends AppController { public function myPage() { $this->startCaching(Configure::get("Blesta.cache_length")); ... } } from the controller i see the function render , is caching template file if is set to , but is not doing any thing to fetch the cached file from cache system if the cahce system is set and the file is cached ? this is projected for next releases or there is no attention to do that in the core system . Quote
Tyson Posted January 21, 2016 Report Posted January 21, 2016 The dispatcher fetches cached templates and then displays them. Depending on what you're doing, this may or may not be what you want, as your controller logic will be bypassed. Consider the following scenario: - Your plugin requires an admin to be logged in, and then displays information only they should see. - Your plugin caches the template that was shown (as mentioned in post 8) when the admin visits the page. Now anyone that visits the URI that the admin went to will see the cached data. You do not need to be logged in, as the dispatcher will display the content immediately, bypassing your controller and any other logic that may exist to verify the user's authentication. So, if you are showing static data that any public user should see, caching like this will be fine. However, if you are looking to restrict access to the template, then you do not want to cache it in this manner. Michael 1 Quote
Michael Posted January 21, 2016 Author Report Posted January 21, 2016 The dispatcher fetches cached templates and then displays them. Depending on what you're doing, this may or may not be what you want, as your controller logic will be bypassed. Consider the following scenario: - Your plugin requires an admin to be logged in, and then displays information only they should see. - Your plugin caches the template that was shown (as mentioned in post 8) when the admin visits the page. Now anyone that visits the URI that the admin went to will see the cached data. You do not need to be logged in, as the dispatcher will display the content immediately, bypassing your controller and any other logic that may exist to verify the user's authentication. So, if you are showing static data that any public user should see, caching like this will be fine. However, if you are looking to restrict access to the template, then you do not want to cache it in this manner. How does one remove a file from the cache mate? Quote
activa Posted January 21, 2016 Report Posted January 21, 2016 How does one remove a file from the cache mate? the emptycache section of amin tools plugin can do the trick ? Michael 1 Quote
Tyson Posted January 21, 2016 Report Posted January 21, 2016 How does one remove a file from the cache mate? If you cached the page as I mentioned in post 8, then you can clear it programmatically via: class MyController extends AppController { public function myPage() { $this->clearCache(); ... } } If the URI differs from the URI of the page you cached, you can specify the cached URI as the first argument to clearCache in order to remove it from the cache. Michael 1 Quote
Blesta Addons Posted January 21, 2016 Report Posted January 21, 2016 I will try it today . Specially in a multilanguages system . Quote
Blesta Addons Posted January 21, 2016 Report Posted January 21, 2016 Tested the caching system of pages , it cache it under the root directory of cache (maybe is a bug in multi-company installs, as it should cache them under the directory id of company) . in a multi-languages system , is useless tool, as it cache the whole page output (including head/body) . so when the user has a different language rathar than the admin , he will see the page in the admin's language (a improvement to cache page for every language is a awesmone feature to add ) . Quote
Blesta Addons Posted January 21, 2016 Report Posted January 21, 2016 maybe another bug found : blesta always fetch the cached page even if we have not initialed the startcahing() function the controller . $this->startCaching(Configure::get("Blesta.cache_length")); PauloV 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.