Using Redis with PHP: Connection, Common Methods, and Application Scenarios
This article explains how to connect to Redis from PHP, demonstrates core Redis commands such as set, get, list, hash, and sorted‑set operations, and describes typical use cases like caching, counters, distributed locks, and message queues.
Connecting to Redis
Before using Redis, you need to connect to the Redis server. In PHP you can use the Redis extension's Redis class. Example code:
// Create a Redis object
$redis = new Redis();
// Connect to Redis server
$redis->connect('127.0.0.1', 6379);The code creates a Redis object and calls connect() to connect to the local server at IP 127.0.0.1 and port 6379.
Common Methods
1. Set and Get Key‑Value Pairs
Redis stores simple key‑value pairs. In PHP you can use set() to store a value and get() to retrieve it. Example:
// Set key "name" to "John"
$redis->set('name', 'John');
// Get the value of key "name"
$name = $redis->get('name');
echo $name; // outputs "John"2. List Operations
Redis lists support left‑push and right‑pop. Use lPush() to push elements to the left and rPop() to pop from the right. Example:
// Push elements to the left of the list
$redis->lPush('tasks', 'task1');
$redis->lPush('tasks', 'task2');
// Pop an element from the right
$task = $redis->rPop('tasks');
echo $task; // outputs "task2"3. Hash Operations
Hashes allow you to store multiple fields under a single key. Use hSet() to set a field and hGet() to retrieve it. Example:
// Set fields in a hash
$redis->hSet('user', 'name', 'John');
$redis->hSet('user', 'age', 20);
// Get a field from the hash
$name = $redis->hGet('user', 'name');
echo $name; // outputs "John"4. Sorted Set Operations
Sorted sets keep members ordered by a score. Use zAdd() to add members and zRange() to retrieve them. Example:
// Add members to a sorted set
$redis->zAdd('scoreboard', 100, 'Alice');
$redis->zAdd('scoreboard', 90, 'Bob');
$redis->zAdd('scoreboard', 80, 'John');
// Retrieve members
$members = $redis->zRange('scoreboard', 0, -1);
foreach ($members as $member) {
echo $member . "
";
}
// Outputs: Alice, Bob, JohnApplication Scenarios
1. Caching
Redis’s high performance makes it ideal for caching hot data, reducing latency by serving frequently accessed data directly from memory.
2. Counters
The incr() method provides an atomic counter useful for tracking visits, sales, etc. Example:
// Increment a counter
$redis->incr('counter');
// Get the current value
$counter = $redis->get('counter');
echo $counter; // prints the counter value3. Distributed Locks
Use setnx() to implement a simple distributed lock, ensuring that only one process can acquire the lock at a time.
4. Message Queues
Redis lists can act as a message queue: producers push messages with lPush() and consumers retrieve them with rPop(), achieving decoupled and reliable message delivery.
Conclusion
The article presented how to use Redis in PHP, covering connection, core commands, and typical use cases such as caching, counters, distributed locks, and message queues, demonstrating how Redis can improve performance and reliability of applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
