I made a view helper that checks if an external URL exists before outputting it. Some of those URLs are in my main layout, so that check is quite slowing down my site by calling all those urls all the times, to check if they exist. I would like to save the output of that function so that it only checks an URL if the same one hasn't been checked already in less than an hour, or a day. I believe I should use Zend Cache to do that? But I have no idea where to start, do you have any suggestions, easy solutions or some basic tutorial to learn? Thanks!
Asked
Active
Viewed 853 times
1 Answers
2
Add global config for cache service, like here:
config/autoload/global.php
'service_manager' => array(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
)
),
config/autoload/cache.global.php
return array(
'caches' => array(
// Cache config
)
)
Use factory to create your View Helper:
Application/Module.php::getViewHelperConfig()
'LinkHelper' => function ($sm) {
$locator = $sm->getServiceLocator();
return new LinkHelper($locator->get('memcached'))
}
Use cache service in your View Helper:
LinkHelper.php
protected $cache;
public function __construct($cache)
{
$this->cache = $cache;
}
public function __invoke($url)
{
$cacheKey = md5($url);
if ($this->cache->hasItem($cacheKey) {
return $this->cache->getItem($cacheKey);
}
$link = ''; // Parse link
$this->cache->setItem($cacheKey, $link);
return $link;
}

Rodin Vasiliy
- 573
- 4
- 7
-
I got this error (I followed your instruction and used the linked configuration): Uncaught exception 'Zend\Cache\Exception\ExtensionNotLoadedException' with message 'Need ext/memcached version >= 1.0.0' – Aise Mar 24 '15 at 15:33
-
1If you don't have `memcached`, then you can just use `filesystem` cache. Example of config you can see [here](http://stackoverflow.com/a/18597132/1129939). And don't forget to make `data/cache` directory writable! – Rodin Vasiliy Mar 24 '15 at 15:41
-
Great! Looks like there are no errors now, but how can I test if it's actually working? – Aise Mar 24 '15 at 19:38
-
Just change mechanism, that checks an external URL, and make sure that value remains old (it should be received from cache). Also you can check your `data/cache` directory - here you'll see some text files – Rodin Vasiliy Mar 24 '15 at 20:03
-
Yes, looks like I got a folder in /cache with a .dat file for each url checked. So I understand that with setItem($cacheKey, $link) creates a subfolder with a $cacheKay.dat file containing $link? Is it possible to have only a single file, instead, with all links inside? Or, at least, to have all those files in a same subfolder? – Aise Mar 24 '15 at 20:45
-
This is a default mechanism of `FileSystem` adapter and I think it will be difficult to change it's logic. Try to play with [parameters](http://framework.zend.com/manual/current/en/modules/zend.cache.storage.adapter.html#the-filesystem-adapter). And, of course, you can write own cache adapter, if you have a lot of time and patience :) – Rodin Vasiliy Mar 24 '15 at 21:20
-
Ehr, I believe it will do for now! Thanks for your help! :) – Aise Mar 24 '15 at 21:26