I have started to work with laravel. It is quite interesting to work. I have started to use the features of laravel. I have started to use redis
by install redis server in my system and change the configuration for redis in app/config/database.php
file. The redis is working fine for the single variables by using set
. i.e.,
$redis = Redis::connection(); $redis->set('name', 'Test');
and i could able to get the value by using
$redis->get('name');
But i want to set the array by using set
function. If i try do that getting the following error
strlen() expects parameter 1 to be string, array given
I have tried by using following codes.
$redis->set('name', array(5, 10)); $values = $redis->lrange('names', array(5, 10));
and if i use
$values = $redis->command('lrange', array(5, 10));
getting the following error
'command' is not a registered Redis command
Can any one explain me the problem and is that possible with redis?...we can set the array values using redis
?
We use various data structures (linked lists, arrays, hashes, etc) in our applications. They are usually implemented in memory but sometimes we need persistence AND speed. This is where in memory DB like Redis can be very useful.
Redis in Laravel In order to use Redis with Laravel, firstly yo will have to install the predis/predis package. You can install it via composer. Just run this command and you're done. Apart from this, you can also install php redis, a php extension via PECL.
All Redis data resides in memory, which enables low latency and high throughput data access. Unlike traditional databases, In-memory data stores don't require a trip to disk, reducing engine latency to microseconds.
Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets, and sorted sets. Before using Redis with Laravel, we encourage you to install and use the phpredis PHP extension via PECL.
Laravel supports the use of Redis, which uses caches for temporary data storage to speed up the process of performing database queries and getting feedback, which will, in turn, reduce the amount of time spent pulling up data. What is a Cache? A cache is a component that stores data so that future requests for the data can be fulfilled quickly.
Hashes are the best way to store objects (Or string key arrays). Lets see how to do this using Laravel. Laravel has a good implementation of Redis as part of the core, we will only need to install a couple of things in order to use it. The documentation is pretty clear about how to do it.
To get started, we need to create a new instance of Laravel on our local machine. We create a new Laravel project from our command line with the name laravel-redis, go into the newly created directory containing our files, and run the application:
The cluster option will instruct the Laravel Redis client to perform client-side sharding across your Redis nodes, allowing you to pool nodes and create a large amount of available RAM. However, note that client-side sharding does not handle failover; therefore, is primarily suited for cached data that is available from another primary data store.
This has been answered in the comments but to make the answer clearer for people visiting in the future.
Redis is language agnostic so it won't recognise any datatype specific to PHP or any other language. The easiest way would be to serialise
/ json_encode
the data on set then unserialise
/json_decode
on get.
Example to store data using json_encode
:
use Illuminate\Support\Facades\Redis; $redis = Redis::connection(); $redis->set('user_details', json_encode([ 'first_name' => 'Alex', 'last_name' => 'Richards' ]) );
Example to retrieve data using json_decode
:
use Illuminate\Support\Facades\Redis; $redis = Redis::connection(); $response = $redis->get('user_details'); $response = json_decode($response);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With