I've been using the filesystem adapter for cacheing data.
E.g..
$cache = StorageFactory::factory(array(
'adapter' => array(
'name' => 'filesystem'
'options' => array('ttl' => 1800, 'cache_dir' => './data/cache'),
),
));
But when using the getItem() function AFTER the TTL clocks over it returns false on success etc, which it should... However, I've noticed that the file remains on the system. Is there a way of forcing the use of the cached file?
Scenario being.. My cache is outdated, when it runs some expensive functions they return nothing or it times out.. So I'd like to use the cache instead!
Just wondering if thats possible?
Thanks!
Here is a useful link to the official ZF2 documentation for the specific StorageAdapter that you are using (filesystem).
Related
we am using Predis to connect to a Redis instance hosted on AWS (Elasticache). We are experiencing performance issues and after having tried other scaling-related solutions, we would like to experiment with adding read replicas in our cluster (with cluster mode disabled, no sharding, just read replicas). Elasticache offers this feature out of the box, but it the documentation of Predis is not very clear on how to use different write/read endpoints.
We currently initialize RedisClient in this way
$redisClient = new RedisClient(['host' => 'the primary endpoint']);
How can we add a read replica endpoint in constructor?
The documentation of PRedis was a bit vague (or outdated). This is how we managed to make it work, in case someone is facing the same issue:
$parameters = [
['host' => $primaryEndpoint, 'role' => 'master', 'alias' => 'master'],
['host' => $replicaEndpoint, 'role' => 'slave', 'alias' => 'slave'],
];
$this->redis = new RedisClient($parameters,
['replication' => true, 'throw_error' => true, 'async' => true]);
The role and alias properties are important.
According to PRedis docs, the replication option should be set as 'replication' => 'predis' but this did not work. Using 'replication' => true did.
I am struggling with this issue for some time.
I am using the sftp adapter to connect to another server where i read/write files a lot.
For thumbnail creation i use background jobs with laravel horizon to retrieve pdf contents from the remote sftp server and then generate a jpg and place in local filesystem.
For first setup i need to make around 150k of thumbnails.
When i use a lot of processes in horizon the remote server can't handle this number of connections.
I must limit to max 2 processes at the moment (10 secs~ * 150k~) not optimal.
I want to cache the connection because i know it is possible and probably solves my problem, but can't get it to work:(
The only reference/tutorial/example/docs i could find is
https://medium.com/#poweredlocal/caching-s3-metadata-requests-in-laravel-bb2b651f18f3
https://flysystem.thephpleague.com/docs/advanced/caching/
When i use the code from the example like this:
Storage::extend('sftp-cached', function ($app, $config) {
$adapter = $app['filesystem']->createSftpAdapter($config);
$store = new Memory();
return new Filesystem(new CachedAdapter($adapter->getDriver()->getAdapter(), $store));
});
I get the error: Driver [] is not supported.
Is there anyone here who can help me a bit further on this?
It appears necessary to adjust your configuration:
In your config/filesystems.php file, add a 'caching' key to your storage:
'default' => [
'driver' => 'sftp-cached',
// ...
'cache' => [
'store' => 'apc',
'expire' => 600,
'prefix' => 'laravel',
],
],
This example is based on official documentation (https://laravel.com/docs/5.6/filesystem#caching), but it is not described well how the 'store' key is used here (where memcached is the example), and you would need to change the implementation of your driver to new Memcached($memcached); (with an instance to inject) instead.
In your case, since the sftp-cached driver implements $store = new Memory();, the cache config must reflect this with 'store' => 'apc' (which is RAM based cache). Available 'store' drivers are found in config/cache.php.
(If you use APC and get an error message Call to undefined function Illuminate\Cache\apc_fetch(), this PHP extension must be installed, see e.g. http://php.net/manual/en/apcu.installation.php)
Finally, I believe the 'prefix' key in config/filesystems.php must be set to the same as the cache key prefix in config/cache.php (which is 'prefix' => 'cache' by default).
We're using APCu as a data cache for PHP on a number of different installations - workstations, development and production servers. Unfortunately, the APCu API appears to be a moving target, and there is little to no official documentation (that I could find). At the moment, we're getting quite different return values for apcu_cache_info()...
With APCu 4.0.1, an entry looks like this:
[
'key' => 'the_entry_key',
'atime' => 1450646021,
'ctime' => 1450646021,
'mtime' => 1450650861,
'dtime' => 0,
// ...
]
With APCu 4.0.7, it looks like this:
[
'info' => 'the_entry_key',
'access_time' => 1450650861,
'creation_time' => 1450646021,
'modification_time' => 1450646021,
'deletion_time' => 0,
// ...
]
According to the source on GitHub, it now looks like this:
[
'info' => 'the_entry_key',
'access_time' => 1450650861,
'creation_time' => 1450646021,
'mtime' => 1450646021,
'deletion_time' => 0,
// ...
]
We've seen other sudden API changes in the past, like when apcu_sma_info() and apcu_cache_info() had to be called with the string "user" as the first parameter - until they didn't. I understand that these changes are related in some way with keeping or dropping compatibility with the old APC extension, but it's getting a little hard to guess how to interact with APCu.
Are these changes documented somewhere, with a version number we can check against? Are there going to be any more changes to this in the near future? How can I get notified about them, other than seeing my application break?
The documentation on php.net has nothing to say about this, and neither does the project's CHANGELOG file. The PHP change log doesn't mention this as a backwards incompatible change either (probably because APCu isn't bundled with PHP by default).
I'm trying to implement a cache using Zend Cache. I use the following code to initialize the caches.
$tagCache = Zend_Cache::factory('Core',
'File',
array('automatic_serialization' => true),
array('cache_dir' => $cfg['instdir']. 'Cache_dir'));
$cache = Zend_Cache::factory('Capture',
'Static',
array(),
array('index_filename' => 'index',
'public_dir' => $cfg['instdir'] . 'Cached',
'tag_cache' => $tagCache));
I use the following code to start caching:
$id = bin2hex($_SERVER["REQUEST_URI"]);
$cache->start($id, array());
The cache files are generated but I can't delete them using the remove() method (the cache doesn't refresh):
$cache->remove($id); // id generated like above from the REQUEST_URI of the page I want to refresh
What am I doing wrong ?
Thanks.
$cache->remove() is a proxy to the backend's remove() method. In this case you are using the Static backend and so we look in there to find out what's happening.
My reading of that method leads me to believe that the $id parameter to remove has to be a filename, so:
$cache->remove('index');
will work.
The more usual way to clear a cache is to use the clean() method though. See the manual.
Symfony2 abstracts Request and Response objects, how I should create Response object, to serve client file attachments, and images for browser display?
You could simply use the Response object:
use Symfony\Component\HttpFoundation\Response;
And set the Content-Type in it that way:
return new Response($image, 200, array('Content-Type' => 'image/png'));
You could use the IgorwFileServeBundle which allows you to break the response abstraction to gain performance. If you are using nginx, you can also take advantage of its XSendfile functionaliy.
Note: Lighttpd and Apache have an alternative XSendfile implementation with some differences and are not supported yet. Pull requests welcome.
Another solution might be this as image is not one of symfony2 supported formats on the Request object.
Also:
static protected function initializeFormats()
{
static::$formats = array(
'html' => array('text/html', 'application/xhtml+xml'),
'txt' => array('text/plain'),
'js' => array('application/javascript', 'application/x-javascript', 'text/javascript'),
'css' => array('text/css'),
'json' => array('application/json', 'application/x-json'),
'xml' => array('text/xml', 'application/xml', 'application/x-xml'),
'rdf' => array('application/rdf+xml'),
'atom' => array('application/atom+xml'),
'rss' => array('application/rss+xml'),
);
}
are Symfony's Request object default available formats. Can check in here.
Good luck!