Php

How to Set Up a Redis Server And Use With PHP

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.

Related Post

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();
}

Recent Posts

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

2 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

2 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

2 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

3 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

3 months ago

Common Practices for Laravel Blade Template

in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More

3 months ago

Categories