Redis
Redis安裝
0. 學習網址
https://www.w3cschool.cn/redis/
1. Redis簡介 ?
??Redis是一個開源(BSD許可),內存存儲的數據結構服務器,可用作數據庫,高速緩存和消息隊列代理。
 ? ?它支持字符串、哈希表、列表、集合、有序集合,位圖,hyperloglogs等數據類型。內置復制、Lua腳本、LRU收回、事務以及不同級別磁盤持久化功能,
 ? ?同時通過Redis Sentinel提供高可用,通過Redis Cluster提供自動分區
? ?學習網址:
? ?https://www.w3cschool.cn/redis/
 ? ?www.redis.cn
 ? ?www.redis.net.cn
? Redis用途:1. 數據庫 2. 緩存
 ? 集群:哨兵、主從、分片式
2. 下載
?? redis-5.0.0.tar.gz(linux)
 ? ?Redis-x64-3.2.100.msi(window安裝版)
 ? ?Redis-x64-3.2.100.zip(window解壓版)
3. 安裝和配置
??3.1 window(略...) ??
?3.2 linux(CentOS)
 ? ? 3.2.1.解壓redis
 ? ? ? ?$ tar -zxvf redis-5.0.0.tar.gz -C /usr/local/
 ? ? ? ?$ tar -zxf redis-5.0.0.tar.gz -C /usr/local/
 ? ? ? ? ? ? ?-zxf 靜默解壓方式
 ? ? ? ?$ cd redis-5.0.0
? ? 3.2.2.安裝gcc
 ? ? ? ?$ yum install gcc
? ? 3.2.3.編譯redis
 ? ? ? ?$ cd /redis-5.0.0
 ? ? ? ?$ make
(You need tcl 8.5 or newer in order to run the Redis test)
yum install -y tcl-devel
 make distclean
redis.windows.config文件(linux對應redis.conf文件)將
①、NETWORK 下 bind 127.0.0.1 注釋掉
②、?并將 protected-mode ?yes ? ?改為 protected-mode ?no;
?③、將daemonize no改為daemonize yes
④、?#requirepass foobared這一行,直接替換掉這行,改為requirepass 密碼?
 requirepass 123456
? ? 3.2.4.檢測安裝情況
 ? ? ? ?$ make install
? ? 3.2.5.修改redis.conf文件
 ? ? ?//允許后臺運行
 ? ? ? ?將daemonize no 改為 daemonize yes
? ? 3.2.6.啟動redis
 ? ? ? ?./redis-server /lky/redis-5.0.0/redis.conf
? ? ? 查看redis進程:ps -ef | grep redis
 ? ? ? 殺掉redis進程:kill -9 進程pid
 ? ? 3.2.7.測試redis啟動是否成功
 ? ? ? ping
? ? 3.2.8.配置成系統服務
? ? ?//? ? 1)配置redis日志文件路徑(可選)
 ? ? ?//? ? logfile "/usr/local/redis-6.0.6/logs/redis.log"
 ??
 ? ? ? 2)新建redis.service文件,配置成系統服務
 ? ? ? vi /usr/lib/systemd/system/redis.service
具體redis.service文件內容配置,詳見redis.service:
[Unit]
 Description=Redis
 After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
 Type=forking
 ExecStart=/usr/redis/src/redis-server /usr/redis/src/redis.conf
 ExecReload=/bin/kill -s HUP $MAINPID?
 ExecStop=/bin/kill -s QUIT $MAINPID?
 PrivateTmp=true
[Install]
 WantedBy=multi-user.target
 ?
? ? 3.2.9.重載系統服務
? ? ? ?systemctl daemon-reload
? ? 3.2.10.啟動redis
 ? ? ? ?systemctl start redis ?#啟動redis服務
 ? ? ? ?systemctl stop redis ? #停止redis服務
 ? ? ? ?systemctl status redis #查看redis狀態
 ? ? ? ?systemctl restart redis #重啟redis服務
 ? ? ? ?systemctl enable redis #注冊服務
 ? ? ? ?systemctl disable redis #注銷服務
4. Redis支持五種數據類型
 
 string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合)
5.通過命令操作redis
redis默認的數據庫有16,mongodb是3個:admin/local/test
5.1 基本命令:
 # redis-cli ? ? ? ? //打開redis終端
 注:配置完成密碼后,以后登錄就密碼按下面的命令進行登錄
 redis-cli -h 127.0.0.1 -p 6379 -a 123456
# ping ? ? ? ? ? ? ?//測試redis是否安裝成功
 # select index ? ? ?//選擇指定的數據庫
5.2 Redis字符串(String)
 # set key ? ? ? ? ?//保存
 # get key ? ? ? ? ?//獲取
 # type key ? ? ? ? //查看類型
 # keys *或keys key //查看所有或者指定的key
 ? ?
 5.3 Redis哈希(Hash)
 Redis hash 是一個string類型的field和value的映射表,hash特別適合用于存儲對象。
 # hset key field1 value1 [field2 value2] ?#同時將多個field-value設置到哈希表key中
 # hget key field ? ? ? ? ? ? ? ? ? ? ? ? ?#獲取指定的字段值
 # hdel key field ? ? ? ? ? ? ? ? ? ? ? ? ?#刪除指定的字段值
 # hgetall key ? ? ? ? ? ? ? ? ? ? ? ? ? ? #查詢指定key的所有字段
 # hexists key field ? ? ? ? ? ? ? ? ? ? ? #查詢指定key中的字段是否存在
 # hlen key ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#獲取指定key中的長度
 5.4 Redis列表(List)
 Redis列表是簡單的字符串列表,按照插入順序排序。你可以添加一個元素到列表的頭部(左邊)或者尾部(右邊)
 # lpush key value1 value2 value3 ? ? ? ? ?#將一個或多個值插入到列表頭部
 # llen key ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#獲取列表的長度
 # lindex key index ? ? ? ? ? ? ? ? ? ? ? ?#根據索引獲取列表中的元素
 # lrange key start sop ? ? ? ? ? ? ? ? ? ?#查看指定范圍內的元素
5.5 Redis集合(Set)
 Redis 的 Set 是 String 類型的無序集合。集合成員是唯一的,這就意味著集合中不能出現重復的數據。
 # sadd key value1 [value2] ? ? ? ? ? ? ? ?#向集合添加一個或多個元素
 # scard key ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #獲取集合中的元素數量
 # sscan key cursor [MATCH pattern] [COUNT count] ? #迭代集合中的元素
 # exists key ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#是否存在
 ?
Redis?命令使用
?Java操作redis
緩存穿透:客戶端向服務器發請求,服務器進入緩存(Redis),緩存中沒有,進入數據庫,數據? ? ? ? ? ? ? ? ? ? 庫中也沒有,(如果客戶端發送大量請求,同樣增加了數據庫壓力)這種情況為:緩? ? ? ? ? ? ? ? ? ? ? 存穿透;
緩存擊穿:假設有一個熱搜,放入數據庫的時候,增加了緩存(Redis)過期時間,當緩存過期? ? ? ? ? ? ? ? ? ? ? ? 后,熱搜依舊在被訪問,客戶端發送的訪問請求由于緩存過期,一下全部發給數據? ? ? ? ? ? ? ? ? ? ? ? ? 庫,增加了數據庫壓力,這種情況為:緩存擊穿;
緩存雪崩:緩存(Redis)內存在運行內存中,(aof能將緩存內存存到本地硬盤中),運行內存? ? ? ? ? ? ? ? ? ? ? 滿了,硬盤壞了,那么緩存(Redis)內存沒有地方放,(服務器沒有問題,但在某一? ? ? ? ? ? ? ? ? ? 時刻或時間段有大量數據涌入緩存,如果設置了過期時間為5分鐘,那么過了5分鐘過? ? ? ? ? ? ? ? ? ? ? 期 了,數據全部消失,這個時候又產生很多數據涌入也稱為緩存雪崩)這種情況為:? ? ? ? ? ? ? ? ? ? ? 緩存雪崩;
服務預熱:主機啟動的時候,提前知道主機里面書籍的類別,有很多人訪問,那么現在服務啟動? ? ? ? ? ? ? ? ? ? ?完成的時候,不要等別人來訪問,先把數據丟到緩存(Redis)里面這種情況為:?服務? ? ? ? ? ? ? ? ? ?預熱;
1、Java操作redis
在eclipse中首先建一個Maven項目
?2、pom.xml中導入依賴
? ? ?<dependency>
 ? ? ? ? <groupId>redis.clients</groupId>
 ? ? ? ? <artifactId>jedis</artifactId>
 ? ? ? ? <version>2.9.0</version>
 ? ? ? </dependency>
3、eclipse的Java代碼操作:
import static org.junit.Assert.*;
import java.util.List;
 import java.util.Map;
 import java.util.Set;
import org.junit.Before;
 import org.junit.Test;
import redis.clients.jedis.Jedis;
 import redis.clients.jedis.ScanResult;
 import redis.clients.jedis.Tuple;
public class RedisTest {
 ?? ?//客戶端
 ?? ?Jedis jedis=new Jedis("192.168.27.110", 6379);
?? ?@Before
 ?? ?public void setUp() throws Exception {
 ?? ??? ?jedis.auth("123456");//設置密碼
 ?? ?}
?? ?@Test
 ?? ?public void test() {
 ?? ??? ?Jedis jedis=new Jedis("192.168.27.110", 6379);
 ?? ??? ?jedis.auth("123456");//設置密碼
 ?? ??? ?System.out.println(jedis.ping());
 ?? ??? ?//redis有16個
 ?? ??? ?jedis.select(15);
 ?? ?}
@Test
 ?? ?public void testBase() {
 ?? ??? ?//jedis.set("name", "小黑");
 ?? ??? ?jedis.setex("name", 10, "小紫");
 ?? ??? ?jedis.expire("name", 10);
 ?? ??? ?jedis.flushAll();
 ?? ??? ?Long l = jedis.ttl("name");
 ?? ??? ?System.out.println(l);
 ?? ?}
 ?? ?
 ?? ?@Test
 ?? ?public void testString() {
 ?? ??? ?jedis.set("yzm", "123");
 ?? ??? ?String string = jedis.get("yzm");
 ?? ?}
 ?? ?
 ?? ?@Test
 ?? ?public void testHash() {
 ?? ??? ?//jedis.hset("user:1001", "name", "小紫");
 ?? ??? ?//jedis.hset("user:1001", "pwd", "admin123");
 ?? ??? ?//jedis.hset("user:1001", "sex", "男");
 ?? ??? ?
 ?? ??? ?//獲得名字
 ?? ??? ?String name = jedis.hget("user:1001", "name");
 ?? ??? ?System.out.println(name);
 ?? ??? ?
 ?? ??? ?//獲得所有的屬性
 ?? ??? ?Map<String, String> user = jedis.hgetAll("user:1001");
 ?? ??? ?for (String s:user.keySet()) {
 ?? ??? ??? ?System.out.println(s+"=>"+user.get(s));
 ?? ??? ?}
 ?? ?}
 ?? ?
 ?? ?@Test
 ?? ?public void testList() {
 ?? ??? ?// jedis.lpush("nums", "1","2","3","4","5");
 ?? ??? ?
 ?? ??? ?//拿數據
 ?? ??? ?List<String> list = jedis.lrange("nums", 0, -1);
 ?? ??? ?list.forEach(System.out::println);
 ?? ??? ?
 ?? ??? ?//拿下表位置的數據
 ?? ??? ?String index = jedis.lindex("nums", 0);
 ?? ??? ?System.out.println(index);
 ?? ?}
 ?? ?
 ?? ?@Test
 ?? ?public void testZSet() {
 ?? ??? ?//jedis.zadd("top", 99999, "周杰倫");
 ?? ??? ?//jedis.zadd("top", 10000011, "伍佰");
 ?? ??? ?//jedis.zadd("top", 10, "🌹");
 ?? ??? ?
 ?? ??? ?/**
 ?? ??? ?Set<String> top = jedis.zrange("top", 0, 2);
 ?? ??? ?for (String s : top) {
 ?? ??? ??? ?System.out.println(s);
 ?? ??? ?}
 ?? ??? ?**/
 ?? ??? ?
 ?? ??? ?// z ?rev(reverse反轉)
 ?? ??? ?/**
 ?? ??? ?Set<String> set = jedis.zrevrange("top", 0, 2);
 ?? ??? ?set.forEach(System.out::println);
 ?? ??? ?**/
 ?? ??? ?ScanResult<Tuple> zscan = jedis.zscan("top", 0);
 ?? ??? ?for (Tuple t : zscan.getResult()) {
 ?? ??? ??? ?System.out.println(t.getScore()+"==>"+t.getElement());
 ?? ??? ?}
 ?? ?}
 }
結束!!!
總結
 
                            
                        - 上一篇: DataGridView很详细的用法
- 下一篇: 用UIpickView实现省市的联动
