Friday, November 18, 2022

How to Set Up a Redis Server And Use With PHP

Here is the article.

This PHP tutorial helps to work the Redis module with php, We will install Redis server into windows and PHP extension of redis into xampp. This tutorial also has a simple PHP script to communicate with redis and php.

I am using redis in PHP application to create a Cache system for faster access to data. This means that Redis is fast, but is also non-volatile.I am using php_redis client but you can use other redis clients as well like Predis.

Redis as an advanced key-value database storage system, Redis can also support complex data structures like strings, hashes, lists, sets, sorted sets, bitmaps and hyperlogs as its key values. It’s often referred to as a NoSQL database.

What is Redis

Redis is an open source(BSD licensed), in-memory data structure store, used as a database, cache and message broker written in C. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with redis queries.

Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

We will download redis for window from Here. You can download redis separately for windows32 and windows 64. You can get redis command information from Redis Commands Docs.

Also checkout other tutorials of PHP Cache,

How To install Redis on Windows

We will follow the following steps to install redis server into windows.

  • Download redis server from the official website.
  • Extract the zip file of the downloaded redis file.
  • Place the unzipped file into d:\redis directory.
  • Run redis-server.exe from d:\redis directory.
  • Then Run redis-cli.exe from d:\redis directory.

How to enable Redis extension in Xampp  

We can enable redis PHP extension by following steps:

  • Download php_redis.dll file from PECL libs. Download the extension as per your current PHP version.
  • Copy the php_redis.dll and paste it to the following folder in XAMPP Server extension directory(D:\XAMPP\php\ext).
  • Open the php.ini file and add extension=php_redis.dll line to this file.
  • Restart XAMPP server
  • Open phpinfo() of XAMPP server and search Redis, if found that means you successfully integrated Redis with PHP.

Simple PHP Redis Example

We will create a simple PHP script that will set the key into redis and get the key value from Redis server. The Redis() method helps to create instance of redis. The $redis->set(key, val) method is used to store value against the key name. The $redis->get(keyname) method is used to get the stored value of the passed key name.

try {
//create redis instance
$redis = new Redis();
//connect with server and port
$redis->connect('localhost', 6379);
//set value
$redis->set('website', 'www.phpflow.com');
//get value
$website = $redis->get('website');
//print www.phpflow.com
echo $website;
} catch (Exception $ex) {
echo $ex->getMessage();
}

No comments:

Post a Comment