1.redis 源码安装,配置,启动服务
1.1redis 6.2.6 下载
https://redis.io/download
上传解压
tar zxvf redis-6.2.6.tar.gz
redis 源码安装
--make
yum -y install gcc-c++ automake autoconf
make MALLOC=libc
--make install
make PREFIX=/home/cch/app/redis install
1.2 redis 配置
redis.conf 配置文件更改
bind 127.0.0.1 -::1
protected-mode no
daemonize yes
vim搜索 / XXX
1.3 redis启动 redis-server /redis-cli
./redis-server redis.conf
ps -ef|grep redis
./redis-cli
ping
set name cch
get cch
2. redis shell命令
redis shell redis 192.168.25.175 6379
ps -ef|grep redis
2.1服务启动
./redis-server redis.config
./redis-cli
redis-cli -h 127.0.0.1 -p 6379 shutdown
ping
select 3
2.2 String
set name zhangshan
get name
mset age 18 addr shanghai
mget age addr
del name
2.3 Hash
hset user name zhangsan
hget user name
hset user age 18
hget user age
hgetall user
hdel user age
del user
2.4 Array
lpush students zhangsan lisi
rpush students wangwu zhaoliu
lrange students 0 3
llen students
lrem students 1 lisi
2.5 Set
sadd letters aaa bbb ccc ddd
smembers letters
scard letters
srem letters aaa
2.6 SortedSet
zadd score 1 zhangsan 5 lisi 3 wangwu 8 zhaoliu
zrange score 0 3
zcard score
zrem score zhangsan
2.7 expire /ttl /set xx /set nx EXISTS
set code test ex 10
get code
ttl code
expire code 10
set code test xx
set code ceshi nx
EXISTS phone
2.8 keys scan
比如这里查询key111开头的key有哪些? 若使用keys命令,则执行 keys 一次性全部查出来
keys key1111*
同样,如果使用scan命令,则用
scan 0 match key1111* count 20
scan 0 match order* count 20
2.9 批量删除
2.10 Lua 脚本 remove_redis_keys.sh
# scriptName remove_redis_keys.sh
REDIS_CLI=/home/cch/app/redis/bin/redis-cli
if [ ${#} -eq 0 ];then
echo "missing params, please tell me which keys would be remove!"
exit;
fi
HOST='127.0.0.1'
PORT=6379
keyPrefix=''
if [ ${#} -gt 2 ];then
HOST=$1
PORT=$2
keyPrefix=$3
elif [ ${#} -gt 1 ];then
HOST=$1
keyPrefix=$2
else
keyPrefix=$1
fi
echo "connecting to ${HOST}....."
sleep 5
CLIENT="${REDIS_CLI} -h ${HOST} -p ${PORT}"
#列出所有的keys
${CLIENT} keys ${keyPrefix}
#开始删除
${CLIENT} keys ${keyPrefix} | xargs ${CLIENT} del
echo "redis_key:[${keyPrefix}] have been removed....."
lua脚本执行
chmod u+x remove_redis_keys.sh
remove_redis_keys.sh 127.0.0.1 6380 U_*
remove_redis_keys.sh 127.0.0.1 U_*
```powershell
sh remove_redis_keys.sh U_*
sh remove_redis_keys.sh order*
2.11 .sh 执行报错 set ff=unix
在linux中用vi打开脚本文件, 在winSCP中使用windows编辑linux脚本,会在每行的结尾多一个\r 在命令模式下输入:
set ff=unix
设置格式为unix格式
3.Springboot集成redis
3.1 RedisConfig.java com.example.miaosha.config
package com.example.miaosha.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String,Object> redisTemplate= new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
@Bean
public DefaultRedisScript<Long> script(){
DefaultRedisScript<Long> redisScript =new DefaultRedisScript<>();
redisScript.setLocation(new ClassPathResource("stock.lua"));
redisScript.setResultType(Long.class);
return redisScript;
}
}
3.2 RedisTemplate 使用 get set 设置超时
@Autowired
private RedisTemplate redisTemplate;
ValueOperations valueOperations = redisTemplate.opsForValue();
String captchaRedisStr=(String) valueOperations.get("captcha:"+user.getId()+":"+goodsId);
redisTemplate.opsForValue().set("captcha:"+user.getId()+":"+goodsId,captcha.text(),300, TimeUnit.SECONDS);
|