Using Redis with PHP for Efficient Data Storage and Retrieval
This tutorial explains how to combine Redis and PHP to store and query data efficiently, covering strings, hashes, lists, sets, and sorted sets with clear code examples for each data structure.
Redis is an in‑memory key‑value store that provides multiple data structures for storing and querying data, while PHP is a widely used backend programming language. This article demonstrates how to use Redis with PHP to efficiently store and retrieve data, including strings, hashes, lists, sets, and sorted sets.
1. String
Strings are the most basic Redis data type and can hold binary data of any length. The example below shows how to connect to Redis, store a string, and retrieve it using PHP.
// 连接Redis服务器
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
// 存储一个字符串
$redis->set('name', 'Tom');
// 获取一个字符串
$name = $redis->get('name');
echo $name; // 输出:Tom2. Hash
Hashes store field‑value pairs and are suitable for representing objects. The following code stores and retrieves a hash representing a user.
// 存储一个哈希
$redis->hset('user', 'name', 'Tom');
$redis->hset('user', 'age', 18);
// 获取一个哈希
$user = $redis->hgetall('user');
print_r($user);3. List
Lists are ordered collections of strings that support push, pop, and range queries. The example demonstrates how to push elements onto a list and retrieve the entire list.
// 存储一个列表
$redis->lpush('list', 'apple');
$redis->lpush('list', 'banana');
// 获取一个列表
$list = $redis->lrange('list', 0, -1);
print_r($list);4. Set
Sets are unordered collections of unique strings and support membership tests as well as set operations such as union and intersection. The code below shows how to add members to a set and retrieve them.
// 存储一个集合
$redis->sadd('set', 'apple');
$redis->sadd('set', 'banana');
// 获取一个集合
$set = $redis->smembers('set');
print_r($set);5. Sorted Set
Sorted sets associate each string element with a score, allowing ordered retrieval based on the score. The example stores two elements with different scores and retrieves them in order.
// 存储一个有序集合
$redis->zadd('sorted_set', 1, 'apple');
$redis->zadd('sorted_set', 2, 'banana');
// 获取一个有序集合
$sortedSet = $redis->zrange('sorted_set', 0, -1);
print_r($sortedSet);Through these examples, we see that combining Redis with PHP enables high‑performance data storage and retrieval. Different data structures suit different scenarios, and Redis also offers advanced features such as transactions, publish/subscribe, and persistence to further improve application performance and reliability.
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.
