sudo apt install redis-server
检查运行状态
sudo systemctl status redis-server
出现以下就是在运行
redis-server.service - Advanced key-value store Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor > Active: activating (start) since Thu 2023-09-07 15:35:19 CST; 9s ago Docs: http://redis.io/documentation, man:redis-server(1) Process: 28954 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=> Tasks: 0 (limit: 18998) Memory: 452.0K CGroup: /system.slice/redis-server.service
启动①redis-server
中文乱码问题
启动Redis服务时,使用 ./redis-cli --raw 命令行。
redis设置值
set key value
多个值
mset key value key value...
设置过期时间
set key value ep(s) ex(s) px(ms)
127.0.0.1:6379> set "pwd" "147055A" ex 60 OK
redis命令行练习
`
./redis-cli`
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set name "jack"
OK
127.0.0.1:6379> get name
"jack"
127.0.0.1:6379> set sex "男"
OK
127.0.0.1:6379> get sex
"\xe7\x94\xb7"
127.0.0.1:6379>
yp@yp-MS-7D22:/usr/bin$ ./redis-cli --raw
127.0.0.1:6379> get sex
男
127.0.0.1:6379> set name "jack"
OK
127.0.0.1:6379> get name
jack
127.0.0.1:6379> mset 1 2 3 4
OK
127.0.0.1:6379> get 1
2
127.0.0.1:6379> get 3
4
127.0.0.1:6379> set 11 15 ex 1
OK
127.0.0.1:6379> get 11127.0.0.1:6379> set "pwd" "147055A" ex 60
OK
127.0.0.1:6379> get pwd
147055A
127.0.0.1:6379> get pwd
147055A
127.0.0.1:6379> keys *
1
pwd
name
3
sex
127.0.0.1:6379> del 1
1
127.0.0.1:6379> keys *
pwd
name
3
sex
127.0.0.1:6379> key pwd
ERR unknown command 'key', with args beginning with: 'pwd'127.0.0.1:6379> EXISTS 3
1
127.0.0.1:6379> RANDOMKEY
sex
127.0.0.1:6379> RANDOMKEY
sex
127.0.0.1:6379> RANDOMKEY
sex
127.0.0.1:6379> rename sex
ERR wrong number of arguments for 'rename' command127.0.0.1:6379> rename sex sex
OK
127.0.0.1:6379> incr 1
1
127.0.0.1:6379> incr 1
2
127.0.0.1:6379> incr 1
3
127.0.0.1:6379> get 1
3
127.0.0.1:6379> get 1
3
127.0.0.1:6379> REnamex sex sexx
ERR unknown command 'REnamex', with args beginning with: 'sex' 'sexx'127.0.0.1:6379> renamex sex sexx
ERR unknown command 'renamex', with args beginning with: 'sex' 'sexx'127.0.0.1:6379> INCR 1
4
127.0.0.1:6379> DECR 1
3
127.0.0.1:6379> DECR 1
2
127.0.0.1:6379> TYPE 1
string
127.0.0.1:6379> TYPE 2
none
127.0.0.1:6379> TYPE 3
string
127.0.0.1:6379> TYPE 4
none
127.0.0.1:6379> TYPE 5
none
127.0.0.1:6379> TYPE 11
none
127.0.0.1:6379> HSET demo 1 2
1
127.0.0.1:6379> get demo
WRONGTYPE Operation against a key holding the wrong kind of value127.0.0.1:6379> HSET demo 3 4
1
127.0.0.1:6379> HGET demo
ERR wrong number of arguments for 'hget' command127.0.0.1:6379> HGET demo 3
4
127.0.0.1:6379> HGET demo 1
2
127.0.0.1:6379> HSET demo "xx" "xxc"
1
127.0.0.1:6379> Hset lu 1 2 3 4 5 6 7 8
4
127.0.0.1:6379> HGETALL lu
1
2
3
4
5
6
7
8
127.0.0.1:6379> Hset wang eat set swimming running skill kill fight
ERR wrong number of arguments for 'hset' command127.0.0.1:6379> Hset wang eat set swimming running
2
127.0.0.1:6379> Hgetall wang
eat
set
swimming
running
127.0.0.1:6379> HEXISTS eat
ERR wrong number of arguments for 'hexists' command127.0.0.1:6379> HEXISTS wang eat
1
127.0.0.1:6379> HEXISTS wang eats
0
127.0.0.1:6379> hvalues
ERR unknown command 'hvalues', with args beginning with:127.0.0.1:6379> hvals wang
set
running
127.0.0.1:6379> hvals lu
2
4
6
8
127.0.0.1:6379> hset lu 23 4 5 6 78 9 89
ERR wrong number of arguments for 'hset' command127.0.0.1:6379> hset lu 23 4 5 6 78
ERR wrong number of arguments for 'hset' command127.0.0.1:6379> hset lu 23 4 5 6
1
127.0.0.1:6379> hvals lu
2
4
6
8
4
127.0.0.1:6379> hlen lu
5
127.0.0.1:6379> hlen wang
2
127.0.0.1:6379> LPUSH demo wo ai ni xiaowang
WRONGTYPE Operation against a key holding the wrong kind of value127.0.0.1:6379> LPUSH demo wo ai ni xiaowang
WRONGTYPE Operation against a key holding the wrong kind of value127.0.0.1:6379> LPUSH demo wo ai ni
WRONGTYPE Operation against a key holding the wrong kind of value127.0.0.1:6379> LPUSH demo wo ai ni
WRONGTYPE Operation against a key holding the wrong kind of value127.0.0.1:6379> LPUSH 1 wo ai niya ua
WRONGTYPE Operation against a key holding the wrong kind of value127.0.0.1:6379> LPUSH c wo ai ni
3
127.0.0.1:6379> LPUSH c wo ai ni ya
7
127.0.0.1:6379> LRANGE c 0 -1
ya
ni
ai
wo
ni
ai
wo
127.0.0.1:6379> linsert wo before xx
ERR wrong number of arguments for 'linsert' command127.0.0.1:6379> linsert ni before wo xx
0
127.0.0.1:6379> linsert ni before ya xx
0
127.0.0.1:6379> LRANGE c 0 -1
ya
ni
ai
wo
ni
ai
wo
127.0.0.1:6379> linsert c before ya xx
8
127.0.0.1:6379> linsert c before ya yy
9
127.0.0.1:6379> linsert c after ya yy
10
127.0.0.1:6379> LRANGE c 0 -1
xx
yy
ya
yy
ni
ai
wo
ni
ai
wo
127.0.0.1:6379> RPOPLPUSH
ERR wrong number of arguments for 'rpoplpush' command127.0.0.1:6379> lindex c 0
xx
127.0.0.1:6379> lindex c 1
yy
127.0.0.1:6379> lindex c 2
ya
127.0.0.1:6379> lindex c 9
wo
127.0.0.1:6379> lindex c 8
ai
127.0.0.1:6379> sadd key 111 222 333 111 222
3
127.0.0.1:6379> sadd key 111 222 333 111 3232 344
2
127.0.0.1:6379> sadd key 222 222 333 111 3232 344
0
127.0.0.1:6379> scard key
5
127.0.0.1:6379> sadd keys 1234 1235 12465 14556
4
127.0.0.1:6379> scard keys
4
127.0.0.1:6379> SISMEMBER keys 111
0
127.0.0.1:6379> SISMEMBER keys 123
0
127.0.0.1:6379> SISMEMBER keys 124
0
127.0.0.1:6379> SISMEMBER keys 1234
1
127.0.0.1:6379> SMEMBERS key
111
222
333
344
3232
127.0.0.1:6379> SMEMBERS keys
1234
1235
12465
14556
127.0.0.1:6379> SDIFF keys key
1234
1235
12465
14556
127.0.0.1:6379> SINTER keys key127.0.0.1:6379> SUNION keys key
111
222
333
344
1234
1235
3232
12465
14556
127.0.0.1:6379> smove key keys
ERR wrong number of arguments for 'smove' command127.0.0.1:6379> smove key 1235 keys
0
127.0.0.1:6379> SPOP keys
12465
127.0.0.1:6379> SPOP keys
14556
127.0.0.1:6379> SREM keys 1234
1
127.0.0.1:6379> ZCARD keys
WRONGTYPE Operation against a key holding the wrong kind of value127.0.0.1:6379> ZCARD key
WRONGTYPE Operation against a key holding the wrong kind of value127.0.0.1:6379> ZRANGE key start stop
ERR value is not an integer or out of range127.0.0.1:6379> ZRANGE keys start stop
ERR value is not an integer or out of range127.0.0.1:6379> save
OK
127.0.0.1:6379> SHUTDOWN
not connected> ./redis-cli
springboot 整合 redis
package com.hidisp;
import com.hidisp.entity.User;
import com.hidisp.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import javax.annotation.Resource;
import java.util.List;
@SpringBootTest
@MapperScan("com.hidisp.mapper")
//如果注入失败 说明没有扫到这个包
class HidispApplicationTests {
@Resource
private UserMapper mapper;
@Resource
RedisTemplate redisTemplate;
Logger logger = LoggerFactory.getLogger(HidispApplicationTests.class);
@Test
void contextLoads() {
List<User> list = mapper.selectList(null);
list.forEach(System.out::println);
logger.info("用户列表被打印出来了。。。");
}
@Test
void context(){
User user = new User();
user.setUid(19);
user.setUname("jadgfgcksongd");
user.setUpwd("password");
user.setBirthday("2017-07-01");
user.setSex("male");
user.setMarry(1);
Integer one = mapper.insert(user);
logger.info("insert successful"+one+"条数据...!");
mapper.selectList(null).forEach(System.out::println);
}
@Test
void testRedis(){
redisTemplate.opsForValue().set("a", "b"); //b
// redisTemplate.opsForHash().put("a", "b","c");
// redisTemplate.opsForSet().add("a", "b", "c","s", "sd");
// redisTemplate.opsForList().leftPush("b", "dsd");
// redisTemplate.opsForList().rightPush("b", "dsds");
}
}
‵
`