0
点赞
收藏
分享

微信扫一扫

redis的学习(二)

分湖芝蘭 2022-04-23 阅读 35
redis

文章目录

一、redis事务?

redis事务

开启事务(multi)
取消事务(discard)//取消事务
执行事务(exec)

二、redis监控

悲观锁:无论做什么都加锁
乐观锁:不加锁,更新数据判断在此期间是否有人修改数据
redis监控

watch money(key)//监视key
unwatch//解除监控

三、jedis操作redis

使用java操作redis,jredis是使用java操作redis的中间件

pom
       <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>4.2.2</version>
        </dependency>
测试
		Jedis jedis = new Jedis("127.0.0.1",6379);
        System.out.println(jedis.ping());
        jedis.close();

四、springboot 使用redis

自己的RedisTemplate模板

@Configuration
public class RedisConfig {
    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory)
    {
        RedisTemplate<String,Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        // json序列化
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(mapper);
        // string序列化
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        //key采用string的序列化
        template.setKeySerializer(stringRedisSerializer);
        //hash采用string的序列化
        template.setHashKeySerializer(stringRedisSerializer);
        //value采用json的序列化
        template.setValueSerializer(jackson2JsonRedisSerializer);
        //hash的value采用json的序列化
        template.setHashKeySerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return  template;
    }
}

五、redis.conf详解

1.redis对大小写不敏感
# units are case insensitive so 1GB 1Gb 1gB are all the same.

包含

2.可以包含多个配置文件
# include /path/to/local.conf
# include /path/to/other.conf

网络

3.绑定ip
bind 127.0.0.1 -::1
4.保护模式
protected-mode yes
5.连接的端口
port 6379

通用

6.守护进程开启默认为no,需要改为yes之后可以后台运行
daemonize yes
7如果以后台方式运行需要指定一个pid文件
pidfile /var/run/redis_6379.pid
8日志的级别
# Specify the server verbosity level.
# This can be one of:
# debug (a lot of information, useful for development/testing))(测试和开发阶段)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)(生成环境默认)
# warning (only very important / critical messages are logged)
loglevel notice
9.生成的日志文件名
logfile ""(默认为直接输出)
10.数据库数量
databases 16(默认为16个)
11.是否显示默认logo
always-show-logo no

快照(持久化)

持久化,在规定时间内执行了多少次操作,则会持久化到文件 .rdb .aof
redis是内存数据库,如果没有持久化,那么就会有数据丢失
1.save
#如果3600s内,如果至少有1个key进行了修改,我们就需要持久化
# save 3600 1
#如果300s内,如果至少有100个key进行了修改,我们就需要持久化
# save 300 100
#如果60s内,如果至少有10000个key进行了修改,我们就需要持久化
# save 60 10000
2.stop-writes-on-bgsave-error yes #如果持久化出错是否继续工作
3.rdbcompression yes #是否压缩rdb文件,需要消耗一些cpu资源
4.rdbchecksum yes #保存rdb文件的时候是否需要错误的检查校验
5.dir ./ #持久化生成的目录

REPLICATION (主从复制)

SECURITY(安全)

可以设置redis密码,默认是没有密码的
# requirepass foobared #配置文件设置密码
命令设置密码
config get requirepass #获取redis密码
config set requirepass 123456 #设置密码
auth 123456 #登录
设置密码之后没有登录显示的错误信息
(error) NOAUTH Authentication required.

CLIENTS(客户端)

# maxclients 10000 #可以连接redis的客户端的数量最大为10000

MEMORY MANAGEMENT(内存)

# maxmemory <bytes> #内存的最大容量
# maxmemory-policy noeviction  #内存达到最大容量后的处理策略

# volatile-lru -> Evict using approximated LRU, only keys with an expire set. #只对设置了过期时间的key进行lru
# allkeys-lru -> Evict any key using approximated LRU. #删除lru算法的key
# volatile-random -> Remove a random key having an expire set.#随机删除即将过期的key
# allkeys-random -> Remove a random key, any key. #随机删除key
# volatile-ttl -> Remove the key with the nearest expire time (minor TTL) #删除即将过期的key
# noeviction -> Don't evict anything, just return an error on write operations.#永不过期,返回错误

APPEND ONLY MOD(aof持久化的配置)

appendonly no #默认是不开启aof模式的,默认是使用rdb方式持久化的,大部分情况下rdb完全够用
appendfilename "appendonly.aof" #aof默认的文件名
# appendfsync always #每次修改都会执行sync,消耗性能
appendfsync everysec #每一秒执行一次sync,可能丢失1s数据
# appendfsync no #不执行sync,这个时候操作系统自己同步数据,速度是最快的

六、持久化

rdb

触发机制

恢复机制

127.0.0.1:6379> config get dir
1) "dir"
2) "/usr/local/bin"#如果在这个目录下的dump.rdb文件,启动就会自动恢复其中的数据
rdb的特点

aof

修复命令

[root@redis bin]# redis-check-aof  --fix  appendonly.aof

重写的规则

aof的特点

举报

相关推荐

0 条评论