Redis
WeeSpeed uses Redis as a cache. After configuring the Redis service parameters in the Environment Variables, you can directly use the Redis class for read and write operations in your code, for example:
$cacheValue = Redis::get('cacheKey');Note
WeeSpeed will automatically connect to the Redis service. You don't need to manually establish a connection.
Please use the static methods of the Redis class to operate Redis.
Redis Class Methods
The Redis class provides the following methods:
1. Key (Key) - Related Instructions
Redis::del(key [, key ...])
Delete one or more keys.
Example:Redis::del("key1", "key2")Redis::exists(key)
Check if a key exists.
Example:Redis::exists("key")Redis::expire(key, seconds)
Set the expiration time (in seconds) for a key.
Example:Redis::expire("key", 10)Redis::ttl(key)
Get the remaining time to live (in seconds) for a key.
Example:Redis::ttl("key")Redis::persist(key)
Remove the expiration time of a key, making it permanently valid.
Example:Redis::persist("key")Redis::keys(pattern)
Find all keys that match the given pattern.
Example:Redis::keys("user:*")Redis::rename(key, newkey)
Rename a key.
Example:Redis::rename("oldkey", "newkey")Redis::type(key)
Get the data type of a key.
Example:Redis::type("key")
2. String - Related Instructions
Redis::set(key, value)
Set the value of a key.
Example:Redis::set("name", "Alice")Redis::get(key)
Get the value of a key.
Example:Redis::get("name")Redis::incr(key)
Increment the value of a key by 1 (only applicable to integers).
Example:Redis::incr("counter")Redis::decr(key)
Decrement the value of a key by 1 (only applicable to integers).
Example:Redis::decr("counter")Redis::append(key, value)
Append a value to the end of the existing value of a key.
Example:Redis::append("name", " Smith")Redis::strlen(key)
Get the length of the string stored in a key.
Example:Redis::strlen("name")
3. Hash - Related Instructions
Redis::hset(key, field, value)
Set the value of a field in a hash table.
Example:Redis::hset("user:1", "name", "Alice")Redis::hget(key, field)
Get the value of a field in a hash table.
Example:Redis::hget("user:1", "name")Redis::hgetall(key)
Get all fields and values in a hash table.
Example:Redis::hgetall("user:1")Redis::hdel(key, field [, field ...])
Delete one or more fields from a hash table.
Example:Redis::hdel("user:1", "name")Redis::hkeys(key)
Get all fields in a hash table.
Example:Redis::hkeys("user:1")Redis::hvals(key)
Get all values in a hash table.
Example:Redis::hvals("user:1")
4. List - Related Instructions
Redis::lpush(key, value [, value ...])
Insert one or more values at the head of a list.
Example:Redis::lpush("mylist", "world")Redis::rpush(key, value [, value ...])
Insert one or more values at the tail of a list.
Example:Redis::rpush("mylist", "hello")Redis::lpop(key)
Remove and return the first element of a list.
Example:Redis::lpop("mylist")Redis::rpop(key)
Remove and return the last element of a list.
Example:Redis::rpop("mylist")Redis::llen(key)
Get the length of a list.
Example:Redis::llen("mylist")Redis::lrange(key, start, stop)
Get the elements within a specified range in a list.
Example:Redis::lrange("mylist", 0, -1)
5. Set - Related Instructions
Redis::sadd(key, member [, member ...])
Add one or more members to a set.
Example:Redis::sadd("myset", "Alice")Redis::smembers(key)
Get all members of a set.
Example:Redis::smembers("myset")Redis::srem(key, member [, member ...])
Remove one or more members from a set.
Example:Redis::srem("myset", "Alice")Redis::sismember(key, member)
Check if a member exists in a set.
Example:Redis::sismember("myset", "Alice")Redis::scard(key)
Get the number of members in a set.
Example:Redis::scard("myset")
6. Sorted Set - Related Instructions
Redis::zadd(key, score, member [, score, member ...])
Add one or more members to a sorted set.
Example:Redis::zadd("myzset", 1, "Alice")Redis::zrange(key, start, stop [, withscores])
Get the members within a specified range in a sorted set (in ascending order of scores).
Example:Redis::zrange("myzset", 0, -1, withscores: true)Redis::zrem(key, member [, member ...])
Remove one or more members from a sorted set.
Example:Redis::zrem("myzset", "Alice")Redis::zscore(key, member)
Get the score of a member in a sorted set.
Example:Redis::zscore("myzset", "Alice")Redis::zcard(key)
Get the number of members in a sorted set.
Example:Redis::zcard("myzset")
7. Publish - Subscribe (Pub/Sub) - Related Instructions
Redis::publish(channel, message)
Publish a message to a specified channel.
Example:Redis::publish("news", "Hello World")Redis::subscribe(channel [, channel ...])
Subscribe to one or more channels.
Example:Redis::subscribe("news")Redis::unsubscribe([channel [, channel ...]])
Unsubscribe from one or more channels.
Example:Redis::unsubscribe("news")
8. Transaction - Related Instructions
Redis::multi
Start a transaction.
Example:Redis::multiRedis::exec
Execute all commands in a transaction.
Example:Redis::execRedis::discard
Cancel a transaction and abandon the execution of all commands.
Example:Redis::discard
9. Server - Related Instructions
Redis::ping
Test if the connection to the server is normal.
Example:Redis::pingRedis::info([section])
Get information and statistics about the server.
Example:Redis::infoRedis::flushdb
Empty all data in the current database.
Example:Redis::flushdbRedis::flushall
Empty all data in all databases.
Example:Redis::flushallRedis::save
Synchronously save data to disk.
Example:Redis::saveRedis::bgsave
Asynchronously save data to disk in the background.
Example:Redis::bgsave