Redis(一)安装使用

本文涉及的产品
云数据库 Tair(兼容Redis),内存型 2GB
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介: Redis(一)安装使用

Redis的安装使用

安装

wget http://download.redis.io/releases/redis-5.0.8.tar.gz
tar -xf redis-5.0.8.tar.gz
cd redis-5.0.8
make
src/redis-server redis.conf #启动,如果需要后台启动需要修改redis.conf -> daemonize yes

src/redis-server redis.conf #启动,如果需要后台启动需要修改redis.conf -> daemonize yes

若环境中没有gcc需要先安装 yum install gcc

若make时出现 fatal error: jemalloc/jemalloc.h: No such file or directory 错误,这是由于redis默认MALLOC=jemalloc 修改为 make MALLOC=libc

密码设置

  • 使用配置文件进行设置密码
#打开redis.conf配置文件
vim redis.conf
#找到requirepass配置,取消注释,修改为自己的密码
requirepass 123456
#重启redis即可生效
  • 使用命令方式设置密码**(重启后失效)**
#进入到redis客户端
[root@centos7lzj redis-5.0.8]# src/redis-cli 
#查看密码
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) ""
#设置密码
127.0.0.1:6379> config set requirepass 123456
OK
#此时密码会立即生效,需要验证,否则报(error) NOAUTH Authentication required.
127.0.0.1:6379> config get requirepass
(error) NOAUTH Authentication required.
#验证
127.0.0.1:6379> auth 123456
OK
#再次查看密码
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "123456"
  • 登录(auth password)
#进入redis客户端
[root@centos7lzj redis-5.0.8]# src/redis-cli 
#尝试ping
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
#此时无权限,登录
127.0.0.1:6379> auth 123456
OK
#再次ping
127.0.0.1:6379> ping
PONG

开启远程连接

#打开配置文件redis.conf
#注释bind配置

API列表

1. String

  APPEND key value
  summary: Append a value to a key
  since: 2.0.0
  BITCOUNT key [start end]
  summary: Count set bits in a string
  since: 2.6.0
  BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL]
  summary: Perform arbitrary bitfield integer operations on strings
  since: 3.2.0
  BITOP operation destkey key [key ...]
  summary: Perform bitwise operations between strings
  since: 2.6.0
  BITPOS key bit [start] [end]
  summary: Find first bit set or clear in a string
  since: 2.8.7
  DECR key
  summary: Decrement the integer value of a key by one
  since: 1.0.0
  DECRBY key decrement
  summary: Decrement the integer value of a key by the given number
  since: 1.0.0
  GET key
  summary: Get the value of a key
  since: 1.0.0
  GETBIT key offset
  summary: Returns the bit value at offset in the string value stored at key
  since: 2.2.0
  GETRANGE key start end
  summary: Get a substring of the string stored at a key
  since: 2.4.0
  GETSET key value
  summary: Set the string value of a key and return its old value
  since: 1.0.0
  INCR key
  summary: Increment the integer value of a key by one
  since: 1.0.0
  INCRBY key increment
  summary: Increment the integer value of a key by the given amount
  since: 1.0.0
  INCRBYFLOAT key increment
  summary: Increment the float value of a key by the given amount
  since: 2.6.0
  MGET key [key ...]
  summary: Get the values of all the given keys
  since: 1.0.0
  MSET key value [key value ...]
  summary: Set multiple keys to multiple values
  since: 1.0.1
  MSETNX key value [key value ...]
  summary: Set multiple keys to multiple values, only if none of the keys exist
  since: 1.0.1
  PSETEX key milliseconds value
  summary: Set the value and expiration in milliseconds of a key
  since: 2.6.0
  SET key value [expiration EX seconds|PX milliseconds] [NX|XX]
  summary: Set the string value of a key
  since: 1.0.0
  SETBIT key offset value
  summary: Sets or clears the bit at offset in the string value stored at key
  since: 2.2.0
  SETEX key seconds value
  summary: Set the value and expiration of a key
  since: 2.0.0
  SETNX key value
  summary: Set the value of a key, only if the key does not exist
  since: 1.0.0
  SETRANGE key offset value
  summary: Overwrite part of a string at key starting at the specified offset
  since: 2.2.0
  STRLEN key
  summary: Get the length of the value stored in a key
  since: 2.2.0

2. hash

  HDEL key field [field ...]
  summary: Delete one or more hash fields
  since: 2.0.0
  HEXISTS key field
  summary: Determine if a hash field exists
  since: 2.0.0
  HGET key field
  summary: Get the value of a hash field
  since: 2.0.0
  HGETALL key
  summary: Get all the fields and values in a hash
  since: 2.0.0
  HINCRBY key field increment
  summary: Increment the integer value of a hash field by the given number
  since: 2.0.0
  HINCRBYFLOAT key field increment
  summary: Increment the float value of a hash field by the given amount
  since: 2.6.0
  HKEYS key
  summary: Get all the fields in a hash
  since: 2.0.0
  HLEN key
  summary: Get the number of fields in a hash
  since: 2.0.0
  HMGET key field [field ...]
  summary: Get the values of all the given hash fields
  since: 2.0.0
  HMSET key field value [field value ...]
  summary: Set multiple hash fields to multiple values
  since: 2.0.0
  HSCAN key cursor [MATCH pattern] [COUNT count]
  summary: Incrementally iterate hash fields and associated values
  since: 2.8.0
  HSET key field value
  summary: Set the string value of a hash field
  since: 2.0.0
  HSETNX key field value
  summary: Set the value of a hash field, only if the field does not exist
  since: 2.0.0
  HSTRLEN key field
  summary: Get the length of the value of a hash field
  since: 3.2.0
  HVALS key
  summary: Get all the values in a hash
  since: 2.0.0

3. list

  BLPOP key [key ...] timeout
  summary: Remove and get the first element in a list, or block until one is available
  since: 2.0.0
  BRPOP key [key ...] timeout
  summary: Remove and get the last element in a list, or block until one is available
  since: 2.0.0
  BRPOPLPUSH source destination timeout
  summary: Pop a value from a list, push it to another list and return it; or block until one is available
  since: 2.2.0
  LINDEX key index
  summary: Get an element from a list by its index
  since: 1.0.0
  LINSERT key BEFORE|AFTER pivot value
  summary: Insert an element before or after another element in a list
  since: 2.2.0
  LLEN key
  summary: Get the length of a list
  since: 1.0.0
  LPOP key
  summary: Remove and get the first element in a list
  since: 1.0.0
  LPUSH key value [value ...]
  summary: Prepend one or multiple values to a list
  since: 1.0.0
  LPUSHX key value
  summary: Prepend a value to a list, only if the list exists
  since: 2.2.0
  LRANGE key start stop
  summary: Get a range of elements from a list
  since: 1.0.0
  LREM key count value
  summary: Remove elements from a list
  since: 1.0.0
  LSET key index value
  summary: Set the value of an element in a list by its index
  since: 1.0.0
  LTRIM key start stop
  summary: Trim a list to the specified range
  since: 1.0.0
  RPOP key
  summary: Remove and get the last element in a list
  since: 1.0.0
  RPOPLPUSH source destination
  summary: Remove the last element in a list, prepend it to another list and return it
  since: 1.2.0
  RPUSH key value [value ...]
  summary: Append one or multiple values to a list
  since: 1.0.0
  RPUSHX key value
  summary: Append a value to a list, only if the list exists
  since: 2.2.0

4. set

SADD key member [member ...]
  summary: Add one or more members to a set
  since: 1.0.0
  SCARD key
  summary: Get the number of members in a set
  since: 1.0.0
  SDIFF key [key ...]
  summary: Subtract multiple sets
  since: 1.0.0
  SDIFFSTORE destination key [key ...]
  summary: Subtract multiple sets and store the resulting set in a key
  since: 1.0.0
  SINTER key [key ...]
  summary: Intersect multiple sets
  since: 1.0.0
  SINTERSTORE destination key [key ...]
  summary: Intersect multiple sets and store the resulting set in a key
  since: 1.0.0
  SISMEMBER key member
  summary: Determine if a given value is a member of a set
  since: 1.0.0
  SMEMBERS key
  summary: Get all the members in a set
  since: 1.0.0
  SMOVE source destination member
  summary: Move a member from one set to another
  since: 1.0.0
  SPOP key [count]
  summary: Remove and return one or multiple random members from a set
  since: 1.0.0
  SRANDMEMBER key [count]
  summary: Get one or multiple random members from a set
  since: 1.0.0
  SREM key member [member ...]
  summary: Remove one or more members from a set
  since: 1.0.0
  SSCAN key cursor [MATCH pattern] [COUNT count]
  summary: Incrementally iterate Set elements
  since: 2.8.0
  SUNION key [key ...]
  summary: Add multiple sets
  since: 1.0.0
  SUNIONSTORE destination key [key ...]
  summary: Add multiple sets and store the resulting set in a key
  since: 1.0.0

5.zset

  BZPOPMAX key [key ...] timeout
  summary: Remove and return the member with the highest score from one or more sorted sets, or block until one is available
  since: 5.0.0
  BZPOPMIN key [key ...] timeout
  summary: Remove and return the member with the lowest score from one or more sorted sets, or block until one is available
  since: 5.0.0
  ZADD key [NX|XX] [CH] [INCR] score member [score member ...]
  summary: Add one or more members to a sorted set, or update its score if it already exists
  since: 1.2.0
  ZCARD key
  summary: Get the number of members in a sorted set
  since: 1.2.0
  ZCOUNT key min max
  summary: Count the members in a sorted set with scores within the given values
  since: 2.0.0
  ZINCRBY key increment member
  summary: Increment the score of a member in a sorted set
  since: 1.2.0
  ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
  summary: Intersect multiple sorted sets and store the resulting sorted set in a new key
  since: 2.0.0
  ZLEXCOUNT key min max
  summary: Count the number of members in a sorted set between a given lexicographical range
  since: 2.8.9
  ZPOPMAX key [count]
  summary: Remove and return members with the highest scores in a sorted set
  since: 5.0.0
  ZPOPMIN key [count]
  summary: Remove and return members with the lowest scores in a sorted set
  since: 5.0.0
  ZRANGE key start stop [WITHSCORES]
  summary: Return a range of members in a sorted set, by index
  since: 1.2.0
  ZRANGEBYLEX key min max [LIMIT offset count]
  summary: Return a range of members in a sorted set, by lexicographical range
  since: 2.8.9
  ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
  summary: Return a range of members in a sorted set, by score
  since: 1.0.5
  ZRANK key member
  summary: Determine the index of a member in a sorted set
  since: 2.0.0
  ZREM key member [member ...]
  summary: Remove one or more members from a sorted set
  since: 1.2.0
  ZREMRANGEBYLEX key min max
  summary: Remove all members in a sorted set between the given lexicographical range
  since: 2.8.9
  ZREMRANGEBYRANK key start stop
  summary: Remove all members in a sorted set within the given indexes
  since: 2.0.0
  ZREMRANGEBYSCORE key min max
  summary: Remove all members in a sorted set within the given scores
  since: 1.2.0
  ZREVRANGE key start stop [WITHSCORES]
  summary: Return a range of members in a sorted set, by index, with scores ordered from high to low
  since: 1.2.0
  ZREVRANGEBYLEX key max min [LIMIT offset count]
  summary: Return a range of members in a sorted set, by lexicographical range, ordered from higher to lower strings.
  since: 2.8.9
  ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
  summary: Return a range of members in a sorted set, by score, with scores ordered from high to low
  since: 2.2.0
  ZREVRANK key member
  summary: Determine the index of a member in a sorted set, with scores ordered from high to low
  since: 2.0.0
  ZSCAN key cursor [MATCH pattern] [COUNT count]
  summary: Incrementally iterate sorted sets elements and associated scores
  since: 2.8.0
  ZSCORE key member
  summary: Get the score associated with the given member in a sorted set
  since: 1.2.0
  ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
  summary: Add multiple sorted sets and store the resulting sorted set in a new key
  since: 2.0.0

Spring Boot 整合Redis

  1. 添加依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>
  1. 配置文件
spring:
  application:
    name: quick-start
  redis:
    database: 0
    timeout: 5000ms
    #单机
    host: 172.20.140.111
    #集群
    #cluster:
      #nodes: 127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381
    port: 6379
    password: 123456
    lettuce:
      pool:
        min-idle: 10
        max-idle: 50
        max-active: 100
        max-wait: 5000ms
  1. 配置类
@Configuration
public class RedisConfiguration {
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String,Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.activateDefaultTyping(om.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringRedisSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
7月前
|
NoSQL Linux Redis
linux安装redis5.0.5
linux安装redis5.0.5
145 1
|
NoSQL Linux Redis
Linux安装Redis3.2.8
Linux安装Redis3.2.8
107 0
|
NoSQL Linux Redis
### 2.2 Redis下载与安装#### 2.2.1 Redis下载
### 2.2 Redis下载与安装#### 2.2.1 Redis下载
119 0
|
NoSQL 数据可视化 Redis
【redis】2.redis可视化工具安装使用
redis可视化工具:Redis Desktop Manager 1.redis桌面管理工具【可视化工具】下载 下载地址:https://redisdesktop.com/download   2.
2247 0
|
5月前
|
NoSQL Redis Windows
windows安装redis
windows安装redis
39 0
|
7月前
|
NoSQL Linux Redis
Linux安装Redis
Linux安装Redis
73 0
|
7月前
|
NoSQL Linux Redis
linux安装redis
linux安装redis
155 0
|
NoSQL Java Redis
34Redis - Redis常用工具下载
34Redis - Redis常用工具下载
98 0
|
NoSQL Redis
32Redis - redis图形化工具安装教程
32Redis - redis图形化工具安装教程
68 0
|
NoSQL Linux 网络安全