One, install the Redis server
Fix it yourself, there's nothing to say.
Two, the Ruby client that installs Redis
The REDIS-RB is used here, self-installing, HTTPS://GITHUB.COM/REDIS/REDIS-RB
Third, connect the server
" Redis " = redis.new
This is the simplest way to connect, other ways to see GitHub
Four, access to data
The methods in the Redis class and the Redis command are one by one corresponding.
So there are only a few examples on GitHub, and there are other ways to refer to Redis commands.
The value of Redis can be a string (string), a hash (MAP), a list, a collection (sets) , and an ordered collection (sorted sets ) and other types
Here is a brief introduction to some commonly used methods.
1, keyword
Redis is key-value type, there are some methods for key
R.del('name')
R.exists ('name')
key
the expiration time (in seconds) of the setting. If the key has expired, it will be automatically deleted and the second parameter in seconds.
R.expire ('name', 60)
- The Expireat function is similar to expire and is used to set the time to live for a key. The difference is that the time parameter accepted by the Expireat command is UNIX timestamp unix timestamp
R.expireat ('name2', time.now.to_i+60)
- Pexpire and expire function Similarly, but it sets the lifetime of the key in milliseconds, rather than in seconds, as in expire.
' name ', 1000000
- Pexpireat is similar to expireat, but it sets the expired Unix timestamp for key in milliseconds, rather than as expireat, in seconds.
' name ', time.now.to_i+1000000
- Returns the remaining lifetime (TTL, Time to live) for a given key, in seconds.
' name '
- Pttl is similar to TTL, but it returns the remaining lifetime of a key in milliseconds, rather than in seconds, as in the case of TTL.
' my_test '
- Removes the given time-to-
key
live and converts the key from "volatile" (with a time-to-live key) to "persistent" (a key without a lifetime and never expires).
R.persist ' name '
- Returns a random key from the current database.
R.randomkey
- Rename the key to Newkey, and if key is the same as Newkey, an error will be returned. If Newkey already exists, the value will be overwritten.
' name ' ' new_name '
- Type returns the types of values stored by key.
return value:
None (key does not exist)
String (String)
List (lists)
Set (SET)
Zset (ordered set)
Hash (hash table)
' name '
- Finds all matches the given pattern
pattern 的 key
.
R.keys (' n*')
2, String
R.set ('name'li')# or r[ ' Sex ' ' male '
R.get ('name')# or r['name')
- Append, if key already exists and the value is a string, then this command appends value to the end of the original value (value). If the key does not exist, it will first create an empty string key, and then perform the append operation, which append similar to the set operation.
' name ' ' 1111 '
- GetRange gets a substring of the value stored on the key
' name ', 1,3
- Mget returns the value of all specified keys. For each key that does not correspond to a string or does not exist, a special value of nil is returned. Because of this, this operation never fails.
' name ' ' Rename '
- Strlen returns the length of the string type value of key. If the key corresponds to a non-string type, an error is returned.
' name '
The following are closely related to statistical relationships:
- INCR adds 1 to the number corresponding to key. If key does not exist, the value corresponding to this key will be set to 0 before the operation. If key has an error type of value or is a string that cannot be represented as a number, an error is returned. This operation is supported on a 64-bit signed integer number.
Reminder: This is a string operation because Redis does not have a dedicated numeric type. The string corresponding to the key is interpreted as a 10-binary, 64-bit signed integer to perform this operation.
Redis stores integers with corresponding integer representations, so for strings that represent numbers, there is no need for additional overhead for string representations of integer storage.
' counter ' ' 123456 ' 'counter'
- Add the number of the key corresponding to decrement. If key does not exist, the key will be set to 0 before the operation. If Key's value type is wrong or is a string that cannot be represented as a number, an error is returned. This operation supports a maximum of 64-bit signed positive digits.
' counter ', 100
- DECR the number corresponding to key minus 1 operation. If key does not exist, the value corresponding to this key will be set to 0 before the operation. If key has an error type of value or is a string that cannot be represented as a number, an error is returned. This operation is supported on a 64-bit signed integer number.
' counter ' ' 123456 ' 'counter'
- Decrby the number corresponding to the key minus decrement. If key does not exist, the key will be set to 0 before the operation. If Key's value type is wrong or is a string that cannot be represented as a number, an error is returned. This operation supports a maximum of 64-bit signed positive digits.
' counter ', 100
Summary:
This article focuses on the processing of key words for Redis Ruby clients, and the common methods for string types of one of five data types.
Redis Ruby Client Learning (i)