redis集群对接及使用

一.项目中涉及的对接redis:

        使用的刚开始是哨兵模式,其中,就需要配置指定的redis连接地址即可,然后可以建立和redis的连接,其中配置如下; 

redis连接配置:

redis:
    database: 0
    password: q&uj?@*Nku
    ssl: false
    pool:
      max-active: 8
      max-idle: 8
      max-wait: -1
      min-idle: 0
    port: 6379
    sentinel:
      master: test-master
      nodes: 1.1.1.1:27001,2.2.2.2:27002,3.3.3.3:27003
    timeout: 0
    enable: false

 

redis连接:

redis连接:

  哨兵模式:

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public Iterable<String> flushCache() throws Exception {
		try {
			Iterable<String> execute = redisTemplate.execute(new RedisCallback<Iterable<String>>() {
				@Override
				public Iterable<String> doInRedis(RedisConnection connection) throws DataAccessException {
					Set<String> keys = new HashSet<String>();
					Cursor<byte[]> cursor  = connection.scan(ScanOptions.scanOptions().count(5).build());
					while (cursor.hasNext()) {
						byte[] key = cursor.next();
						keys.add(new String(key, StandardCharsets.UTF_8));
						connection.del(key);
					}
					return keys;
				}
			});
			return execute;
		} catch (Exception e) {
			LoggerUtil.error("DataDicService flushCache error:{}", e);
			throw e;
		}
	}

 

 

 

集群模式:

redis配置:

  redis:
    clusterUrl: http://testcache.test.com/cache/client/redis/cluster/10003.json?clientVersion=1.0-SNAPSHOT
    timeout: 0
    enable: true

 

redis结点通过调用api来解析对应得节点:

连接通过集群模式来连接:

	
    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    public Iterable<String> flushCache() throws Exception {
		try {
			Iterable<String> execute = redisTemplate.execute(new RedisCallback<Iterable<String>>() {

				@Override
				public Iterable<String> doInRedis(RedisConnection connection) throws DataAccessException {
					Set<String> keys = new HashSet<String>();
					//集群模式
					Set<byte[]> byteKeys = connection.keys(("TestRedisKey*").getBytes());
					byteKeys.stream().forEach(byteKey ->{
						keys.add(new String(byteKey, StandardCharsets.UTF_8));
						connection.del(byteKey);
					});
					return keys;
				}

			});

			return execute;
		} catch (Exception e) {
			LoggerUtil.error("DataDicService flushCache error:{}", e);
			throw e;
		}
	}

 

在flushCash得时候注意区分。。。。

 

 

 

二.redis数据存储的五大类型及使用方法:(String,Hash,Set,List,Zset):

@Autowired
private RedisTemplate<String, String> redisTemplate;

 

redisTemplate的excute和delete等API的使用。

 

String(字符串):

redisTemplate.opsForValue()对应set,get,append,size,increment等API

 

Hash(哈希):

redisTemplate.opsForHash()对应put,get,keys,values,size,putAll,increment,delete等API

 

List(列表):push是推送到初始位置,pop是追加到最末位置

redisTemplate.opsForList()对应leftPush,letfPop,range,remove,size,trim,leftPushAll等API

 

Set(集合):

redisTemplate.opsForSet()对应add,move,size,pop等API

 

Zset(有序集合):

redisTemplate.opsForSet()对应add,count,range,remove,scan,size等API

 

 

根据实际情况使用对应的数据类型。。。。

 

 

 

三.redis的置换策略:

    当redis的数据量达到maxMemory时,就会采用指定的置换策略(maxMemory-policy)。

    提到置换策略先介绍下LRU算法,如其名Least Recently Used(最近最不常使用的)

    常见的置换策略:

    3.1 noeviction ------不进行置换,达到最大内存,后入的数据之间报错

    3.2 allkeys-lru ------对所有的key进行LRU算法置换,淘汰掉最近不常使用的key值

    3.3 allkeys-random ------对所有的key进行随机置换

    3.4 volatile-lru ------对有生命周期的key进行LRU算法置换

    3.5 volatile-random ------对有生命周期的key进行随机置换

    3.6 volatile-ttl ------对有生命周期的key,置换出TTL(存活时间)最短的

 

四.redis的持久化策略:

    4.1 RDB持久化:

            在指定的时间间隔内将数据集快照写入磁盘中(原理即是将内存中数据记录定时,dump到磁盘中的RDB持久化方式)-----保存到dump.rdb文件中----fork的方式不断更新,适合灾备

    4.2 AOF持久化:

            以日志的形式记录服务器的每一个set和delete操作,查询操作不会记录(原理即是将redis的操作记录追加到日志中,只包含写和删除的操作)

 

        

 

 

转载于:https://my.oschina.net/u/3110937/blog/1786167

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值