redis學習筆記(二)JedisCluster + redis 3.2.5叢集
redis系列文章目錄
開發環境
- jedis2.9.0
- redis3.2.5
- springboot 1.3.6.RELEASE
注意事項:本文搭建的redis配置了密碼,所以jedis客戶端要使用比較新的版本,老版本不支援密碼,需要修改原始碼比較麻煩
環境搭建
- 引入jedis
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
- 注入
JedisCluster
import com.le.luffi.media.common.Constant;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import java.util.HashSet;
import java.util.Set;
@Configuration
publicclassJedisClusterConfig{
@Bean
publicJedisCluster getJedisCluster(){
String[] serverArray =Constant.REDIS_CACHE_CLUSTER_NODES.split(",");
Set<HostAndPort> nodes =newHashSet<>();
for(String ipPort : serverArray){
String[] ipPortPair = ipPort .split(":");
nodes.add(newHostAndPort(ipPortPair[0].trim(),Integer.valueOf(ipPortPair[1].trim())));
}
JedisCluster jedisCluster =newJedisCluster(nodes,Constant.REDIS_CACHE_COMMANDTIMEOUT,Constant.REDIS_CACHE_SOTIMEOUT,Constant.REDIS_CACHE_MAXATTEMPTS,Constant.REDIS_CACHE_CLUSTER_PASSWORD,newGenericObjectPoolConfig());
return jedisCluster;
}
}
- 建立
JedisTemplate
模板
```java
package com.le.luffi.media.redis;
import com.le.luffi.media.common.Constant;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisCluster;
@Component
public class JedisTemplate {
private static final Logger LOGGER = LoggerFactory.getLogger(JedisTemplate.class);
@Autowired
privateJedisCluster jedisCluster;
privatestaticfinalString KEY_SPLIT =":";//用於隔開快取字首與快取鍵值
/**
* 設定快取
* @param prefix 快取字首(用於區分快取,防止快取鍵值重複)
* @param key 快取key
* @param value 快取value
*/
publicvoidset(String prefix,String key,String value){
if(StringUtils.isBlank(prefix))thrownewIllegalArgumentException("prefix must not null!");
if(StringUtils.isBlank(key))thrownewIllegalArgumentException("key must not null!");
jedisCluster.set(prefix + KEY_SPLIT + key, value);
LOGGER.debug("RedisUtil:set cache key={},value={}", prefix + KEY_SPLIT + key, value);
}
/**
* 設定快取,並且自己指定過期時間
* @param prefix
* @param key
* @param value
* @param expireTime 過期時間
*/
publicvoid setWithExpireTime(String prefix,String key,String value,int expireTime){
if(StringUtils.isBlank(prefix))thrownewIllegalArgumentException("prefix must not null!");
if(StringUtils.isBlank(key))thrownewIllegalArgumentException("key must not null!");
jedisCluster.setex(prefix + KEY_SPLIT + key, expireTime, value);
LOGGER.debug("RedisUtil:setWithExpireTime cache key={},value={},expireTime={}", prefix + KEY_SPLIT + key, value,
expireTime);
}
/**
* 設定快取,並且由配置檔案指定過期時間
* @param prefix
* @param key
* @param value
*/
publicvoid setWithExpireTime(String prefix,String key,String value){
if(StringUtils.isBlank(prefix))thrownewIllegalArgumentException("prefix must not null!");
if(StringUtils.isBlank(key))thrownewIllegalArgumentException("key must not null!");
// int EXPIRE_SECONDS = redisProperties.getExpireSeconds();
jedisCluster.setex(prefix + KEY_SPLIT + key, Constant.REDIS_CACHE_EXPIRESECONDS, value);
LOGGER.debug("RedisUtil:setWithExpireTime cache key={},value={},expireTime={}", prefix + KEY_SPLIT + key, value, Constant.REDIS_CACHE_EXPIRESECONDS);
}
/**
* 獲取指定key的快取
* @param prefix
* @param key
*/
publicStringget(String prefix,String key){
if(StringUtils.isBlank(prefix))thrownewIllegalArgumentException("prefix must not null!");
if(StringUtils.isBlank(key))thrownewIllegalArgumentException("key must not null!");
String value = jedisCluster.get(prefix + KEY_SPLIT + key);
LOGGER.debug("RedisUtil:get cache key={},value={}", prefix + KEY_SPLIT + key, value);
return value;
}
/**
* 刪除指定key的快取
* @param prefix
* @param key
*/
publicvoid deleteWithPrefix(String prefix,String key){
if(StringUtils.isBlank(prefix))thrownewIllegalArgumentException("prefix must not null!");
if(StringUtils.isBlank(key))thrownewIllegalArgumentException("key must not null!");
jedisCluster.del(prefix + KEY_SPLIT + key);
LOGGER.debug("RedisUtil:delete cache key={}", prefix + KEY_SPLIT + key);
相關推薦
redis學習筆記(二)JedisCluster + redis 3.2.5叢集
redis系列文章目錄
開發環境
jedis2.9.0redis3.2.5springboot 1.3.6.RELEASE
注意事項:本文搭建的redis配置了密碼,所以jedis客戶端要使用
Redis學習筆記(二)——Redis資料型別
歡迎訪問:zrxJuly’s Bolg
Redis支援5種資料型別: - string:字串 - hash:雜湊 - list:列表 - set: 集合 - zset:有序集合(sorted set)
String:字串
string是Red
java連線redis資料庫-redis學習筆記(二)
資料庫安裝好之後,接下來就是怎麼去使用伺服器,怎麼像連線關係型資料庫一樣去連線和操作資料。以下介紹java連線redis資料庫並實現操作
在連線redis資料庫之前需要對redis,做如下設定:
1、在配置檔案redis.conf中把繫結的Ip
JAVA之 Redis 學習筆記(二) Redis的連線IP、埠號、連線密碼的修改以及與JAVA簡單互動
一、專案中引入Redis包
根據上一篇文章中給的jar包路徑,將jar包下載,匯入自己的專案。(jedis-2.7.2.jar)
二、與Redis連線
(1)連線IP和埠
Redis學習筆記(二)— 在linux下搭建redis伺服器
搭建環境:linux是centos7.4(請注意centos7以下版本的防火牆跟centos7以上的會有所區別,使用redis客戶端連線redis時會有區別,建議使用centos7以上版本)一、下載linux版本的redis二、安裝1、使用SecureCRT工具將redis壓
Redis學習筆記(1):Redis的說明與安裝
sets cti ansi c sde pos AR bsd 學習 ash Redis學習筆記(1):Redis說明的安裝
說明
什麽是Redis
REmote DIctionary Server(Redis) 是一個由Salvatore Sanfilippo寫的key-v
Redis學習筆記(一)——初識Redis
歡迎訪問:zrxJuly’s Blog
Redis介紹
Redis是一個開源、高階的鍵值儲存和一個適用的解決方案,用於構建高效能、可擴充套件的web應用程式,遵循BSD協議[1],是一個高效能的key-value資料庫。
Redis特點:
Redis 學習筆記(四):redis 面試題總結
目錄:
Redis 的好處
相比 memcached 的優勢
常見效能問題
記憶體淘汰策略
使用的場景
1. 使用Redis有哪些好處?
(1)
速度快,因為資料存在記憶體中,類似於HashMap,HashMap的優勢就是查詢和操作的時間複雜度都是O(1)
(2)
Redis深入學習筆記(二)client list 命令詳解
學習 字節數組 height 要求 ddr idt 設置 分配 分組 Redis的client list 命令可以獲取當前連接到redis server端的所有客戶端以及相關狀態,本篇主要介紹每一個參數的作用。
clisnt list 命令輸出結果如下:
(1)標識:id
Redis學習筆記(五)jedis(JedisCluster)操作Redis叢集 redis-cluster
redis系列文章目錄
版本說明
jedis2.9.0
redis3.2.5
參考資料:redis命令參考。 不過這個稍微有一點點老
java(JedisCluster)操作redis叢集
這裡只是幾個簡單的demo,直接
Redis學習筆記(二十) 釋出訂閱(下)
當一個客戶端執行SUBSCRIBE命令訂閱某個或某些頻道時,這個客戶端與被訂閱頻道之間就建立起了一種訂閱關係。
Redis將所有頻道的訂閱關係儲存在伺服器狀態的pubsub_channels字典裡面,這個字典的鍵是某個被訂閱的頻道,而鍵的值是一個連結串列,連結串列裡面記錄了所有訂閱這個頻道的客戶端:
str
Redis學習筆記(一)關於在windows64位環境下的安裝學習使用
客戶端 mas key-value 錯誤 services 再次 基準 alt 類型 前言
由於工作需要,目前我正在學習使用Redis。我當時學習Redis就從網上下載了點資料就開始學習了。入門看的是《REDIS入門指南》,這本書個人覺得很適合新手用來學習接觸。根據書上的引
Redis學習筆記(三)常用命令整理
mes ember nbsp end 插入 學習筆記 頻道 hash value Redis 常用命令
1.DEL key 刪除key2.EXISTS key 檢查key是否存在3.KEYS * 查看所有的key4.EXPIRE key seconds 設置key的過期時
redis學習筆記(14)---redis基本命令總結
del diff lan 命令 列表 對象 很多 順序 reg http://doc.redisfans.com/ 網頁,對所有redis命令的用法與示例進行了詳細的描述
概述
Redis的鍵值可以使用物種數據類型:字符串,散列表,列表,集合,有序集合。本文詳細介紹這
redis學習筆記(三)
lis 記錄 筆記 style pytho font 通過 num 獲取 列表類型
List,可以存儲一個有序的字符中列表,列表內元素非唯一,可以向兩端加入元素,或者獲得列表的一個片段
內部使用雙向鏈表實現,兩端添加元素負責度O(1),通過索引訪問的速度較慢
可以用在分頁,
Redis學習筆記(四)——數據結構之List
ltr dex pan 返回 red 操作 數據結構 alt 區間 一、介紹
Redis列表(List)是簡單的字符串列表,按照插入順序排序。你可以添加一個元素到列表的頭部(left)或者尾部(right),一個列表最多可以包含232-1個元素(4294967295,每
Redis學習筆記(八)——持久化
配置信息 成功 一個 filename 自動啟用 復數 裏的 ont 安全性 一、介紹
Redis的所有的數據都存在內存中,然後不定期的通過異步方式保存到磁盤上(這稱為"半持久化模式");也可以把每一次數據變化都寫入到一個append only file(aof)裏面(
Redis學習筆記(一)---Redis的五種資料型別的簡單介紹和使用
1.準備工作:
1.1在Linux下安裝Redis
https://www.cnblogs.com/dddyyy/p/9763098.html
1.2啟動Redis
先把root/redis的redis.conf放到 /usr/local/redis/
Redis學習筆記(1)—— Redis簡介
一、NoSQL概述
1.1 什麼是NoSQL
NoSQL(NoSQL = Not Only SQL),意指“不僅僅是SQL”,是一項全新的資料庫理念,泛指非關係型的資料庫。
1.2 為什麼需要NoSQL
隨著網際網路web2.0網站的興起,非關係型的資料庫成了一個極其熱門的新領域,非關係型資料
Redis 學習筆記(1)—— Redis安裝,String 型別測試
1 Redis 介紹
1.1 概述
Redis是一個開源,先進的key-value儲存,並用於構建高效能,可擴充套件的應用程式的完美解決方案。
Redis資料庫完全在記憶體中,使用磁碟僅用於永續性。
相比許多鍵值資料儲存,Redis擁有一套較為豐富的資料型別。