How to Store and Retrieve Data Using Memcached in PHP/Laravel?
Memcached is a popular, open-source, distributed memory object caching system that can significantly improve the performance of web applications by reducing the number of times an application needs to access the database. Memcached stores data in RAM, making it much faster to retrieve data compared to database queries. In this answer, we will discuss how to store and retrieve data using Memcached in PHP with Laravel.
First, let's make sure that Memcached is installed and running on your system. You can download Memcached from its official website (https://memcached.org/downloads) and follow the installation instructions for your operating system. Once Memcached is installed, you can check if it's running by using the following command in your terminal or command prompt:
memcached -v
This command should display the Memcached version number and other information.
Next, let's set up Memcached in Laravel. To use Memcached with Laravel, you need to install the Laravel Memcached driver. You can install it using Composer by running the following command:
composer require predis/predis
This command installs the Predis PHP library, which is a PHP client for Memcached and Redis. Once the installation is complete, you need to configure Memcached in your Laravel .env
file by adding the following lines:
CACHE_DRIVER=memcached
MEMCACHE_HOST=127.0.0.1
MEMCACHE_PORT=11211
Replace 127.0.0.1
and 11211
with the IP address and port number of your Memcached server if it's not running on the same machine as Laravel.
Now that Memcached is set up in Laravel, let's discuss how to store and retrieve data using Memcached.
To store data in Memcached, you can use the cache
helper function provided by Laravel. Here's an example of how to store a string in Memcached:
$key = 'my_key';
$value = 'Hello, Memcached!';
// Store the value in Memcached with a default expiration time of 1 hour
Cache::put($key, $value, 3600);
You can also store an associative array in Memcached by using the forever
method instead of put
:
$key = 'my_key';
$value = [
'name' => 'John Doe',
'age' => 30,
];
// Store the value in Memcached with an infinite expiration time
Cache::forever($key, $value);
To retrieve data from Memcached, you can use the get
method provided by Laravel:
$key = 'my_key';
// Retrieve the value from Memcached
$value = Cache::get($key);
// Output the value
echo $value; // Hello, Memcached!
If the key does not exist in Memcached, the get
method will return null
. You can also check if a key exists in Memcached by using the has
method:
$key = 'my_key';
// Check if the key exists in Memcached
if (Cache::has($key)) {
// The key exists, retrieve the value
$value = Cache::get($key);
// Output the value
echo $value; // Hello, Memcached!
} else {
// The key does not exist, do something else
echo 'Key not found in Memcached';
}
You can also delete a key from Memcached by using the forget
method:
$key = 'my_key';
// Delete the key from Memcached
Cache::forget($key);
That's it! You now know how to store and retrieve data using Memcached in PHP with Laravel. Memcached can significantly improve the performance of your Laravel application by reducing the number of database queries and serving frequently accessed data from memory.