日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

redis-4.0.10集群安装(3台机器,6个node),以及在Spring项目中的集成,redis操作工具类

發(fā)布時(shí)間:2024/9/27 javascript 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 redis-4.0.10集群安装(3台机器,6个node),以及在Spring项目中的集成,redis操作工具类 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1 Redis安裝

redis高可用的三種常見(jiàn)的集群方式:redis sentinel 、redis cluster(多主機(jī)+分布式)、redis sharding。接下來(lái)主要介紹redis sentinel的部署過(guò)程。

1.1 Redis集群安裝 (3臺(tái)機(jī)器,6個(gè)node)

1.1.1安裝前準(zhǔn)備

yum install -y gcc make MALLOC=libc

1.1.2 下載redis軟件,解壓

將redis上傳至/home/bigdata/software,解壓redis

cd /home/bigdata/software tar -zxvf redis-4.0.10.tar.gz -C /home/bigdata/installed cd /home/bigdata/installed/redis-4.0.10

1.1.3.創(chuàng)建想要安裝的目錄:

mkdir -p /usr/local/redis/redis-4.0.10

1.1.3.編譯:

make PREFIX=/usr/local/redis/redis-4.0.10 install(root權(quán)限或sudo)

1.1.4.源碼目錄拷貝redis.conf配置文件

cp redis.conf /usr/local/redis/ redis-4.0.10/redis.conf(可以 在解壓的tar.gz包里找到默認(rèn)的redis.conf

1.1.5.添加環(huán)境變量到/etc/profile

#redis export REDIS_HOME=/usr/local/redis/redis-4.0.10 export PATH=$PATH:$REDIS_HOME/bin

配置完成之后,退出,執(zhí)行:

source /etc/profile

1.1.6.修改redis配置文件

a、daemonize:改成yes,允許以守護(hù)進(jìn)程方式啟動(dòng),必改。 b、bind:默認(rèn)僅允許本機(jī)連上,注釋掉以便讓任何機(jī)器都可以連(為了安全開啟密碼) c、requirepass改下密碼 (test_redis,集群的話暫時(shí)不配置) d、port:默認(rèn)6379即可。默認(rèn)端口會(huì)比較方便,無(wú)需改。 e、dir:改成/usr/local/redis/redis-4.0.10/standalone 指定rdb、aof文件的生成目錄,dir不能指定為文件。可用相對(duì)和絕對(duì)路徑。另外相對(duì)路 徑是相對(duì)于執(zhí)行啟動(dòng)腳本的目錄。配置成./data,在哪里執(zhí)行啟動(dòng)命令,就在哪里找./data目 錄,如果找不到則不能啟動(dòng)。這里寫絕對(duì)路徑會(huì)好點(diǎn)。 f、logfile:可用指定為空。也可以指定到"/usr/local/redis/redis-4.0.10/standalone/6379.log"(文件名隨意取會(huì)自動(dòng)產(chǎn)生) g、dbfilename:rdb的文件名,改成dump-6379.rdb (產(chǎn)生的路徑隨dir的配置) h、appendonly改成yes i、appendfilename改成appendonly-6379.aof (產(chǎn)生的路徑隨dir的配置)

啟動(dòng)redis:

[root@bigdata1 redis-4.0.10]# bin/redis-server /usr/local/redis/redis-4.0.10/redis.conf

查看redis進(jìn)程:

[root@bigdata1 redis-4.0.10]# ps -ef | grep redis root 13079 1 0 14:07 ? 00:00:00 bin/redis-server *: root 13096 11631 0 14:07 pts/2 00:00:00 grep --color=auto r [root@bigdata1 redis-4.0.10]#

連接Redis:

[root@bigdata1 redis-4.0.10] cd /usr/local/redis/redis-4.0.10 [root@bigdata1 redis-4.0.10]# bin/redis-cli -h bigdata1 -p 6379 -a test_redis Warning: Using a password with '-a' option on the command line interface may not be safe. bigdata1:6379>

通過(guò)此部,可以知道單個(gè)redis集群安裝完成。

關(guān)閉Redis:

方式一:直接kill進(jìn)程 方式二:redis-cli shutdown(無(wú)密碼)或 redis-cli a test_redis shutdown(有密 碼)su - root -c 'redis-server /usr/local/redis/redis-4.0.10/redis.conf'

1.2集群安裝:

修改/usr/local/redis/redis-4.0.10/redis.conf中的內(nèi)容,改變下面的內(nèi)容

cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 protected-mode no

在三臺(tái)機(jī)器上分別復(fù)制/usr/local/redis/redis-4.0.10 為 /usr/local/redis/redis-4.0.10-2
將里面的port 改成6378,所有的/usr/local/redis/redis-4.0.10-2/redis.conf 中的redis-4.0.10改成redis-4.0.10-2

安裝ruby

[root@bigdata1 software]# cd /home/bigdata/software [root@bigdata1 software]# tar -zxvf ruby-2.3.3.tar.gz -C /home/bigdata/installed/ [root@bigdata1 software]# cd /home/bigdata/installed/ruby-2.3.3 [root@bigdata1 ruby-2.3.3]# ./configure --prefix=/usr/local/ruby/ruby-2.3.3 [root@bigdata1 ruby-2.3.3]# make #耗時(shí)長(zhǎng) [root@bigdata1 ruby-2.3.3]#y make install

配置ruby的環(huán)境變量:

[root@bigdata1 ruby-2.3.3]# vim /etc/profile# ruby export RUBY_HOME=/usr/local/ruby/ruby-2.3.3 export PATH=$PATH:$RUBY_HOME/binsource /etc/profile

安裝rubygems:
進(jìn)入rubygems官網(wǎng),下載最新的rubygems包。

https://rubygems.org/pages/download#formats

進(jìn)入

cd /home/bigdata/installed unzip rubygems-2.7.7.zip [root@bigdata2 installed]# cd rubygems-2.7.7/

然后安裝,命令如下:

ruby setup.rb

2)查看ruby v是否能到版本,能得到版本說(shuō)明安裝好了,同時(shí)表示ruby加入到環(huán)境變 量中了
3)安裝好ruby后還要安裝openssl,否則嘗試gem install redis時(shí)會(huì)出現(xiàn)如下錯(cuò)誤
Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non‐HTTPS sources,則到Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non‐HTTPS sources

4)在安裝openssl之前,執(zhí)行如下命令,輸入y同意

yum install --enablerepo=centosplus openssl-devel

5)進(jìn)入ruby2.3.1目錄的ext/openssl目錄,執(zhí)行ruby extconf.rb

[root@bigdata1 ruby-2.3.3]# cd /home/bigdata/installed/ruby-2.3.3/ext/openssl [root@bigdata1 openssl]# ruby extconf.rb [root@bigdata1 openssl]# make [root@bigdata1 openssl]# make install [root@bigdata1 openssl]# openssl version查看openssl的版本,證明安裝好了

6)使用gem安裝redis

[root@bigdata2 bin]# pwd /home/bigdata/installed/rubygems-2.7.7/bin [root@bigdata2 bin]# ./gem install redis --version 4.0.10

執(zhí)行redis的創(chuàng)建集群命令創(chuàng)建集群
進(jìn)入redis的源碼目錄:

[root@bigdata1 src]# cd /usr/local/redis/redis/src

將redis-trib.rb拷貝到redis的安裝目錄(3臺(tái)機(jī)器上分別執(zhí)行下面的操作):

[root@bigdata1 src]# cp redis-trib.rb /usr/local/redis/redis-4.0.10 [root@bigdata1 src]# cp redis-trib.rb /usr/local/redis/redis-4.0.10-2

查看redis-trib.rb的用法

[root@bigdata2 src]# ./redis-trib.rb Usage: redis-trib <command> <options> <arguments ...>create host1:port1 ... hostN:portN--replicas <arg> check host:portinfo host:portfix host:port--timeout <arg> reshard host:port--from <arg> --to <arg> --slots <arg> --yes --timeout <arg> --pipeline <arg> rebalance host:port--weight <arg> --auto-weights --use-empty-masters --timeout <arg> --simulate --pipeline <arg> --threshold <arg> add-node new_host:new_port existing_host:existing_port--slave --master-id <arg> del-node host:port node_idset-timeout host:port millisecondscall host:port command arg arg .. argimport host:port--from <arg> --copy --replace help (show this help)For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster. [root@bigdata2 src]#

啟動(dòng)各各redis節(jié)點(diǎn),然后啟動(dòng)集群
下面的xxx.xxx.xxx.為ip的前綴

[root@bigdata1 src]#./redis-trib.rb create --replicas 1 xxx.xxx.xxx.140:6379 xxx.xxx.xxx.140:6378 xxx.xxx.xxx.141:6379 xxx.xxx.xxx.141:6378 xxx.xxx.xxx.142:6379 xxx.xxx.xxx.142:6378 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: bigdata1:6379 bigdata2:6379 bigdata3:6379 Adding replica bigdata2:6378 to bigdata1:6379 Adding replica bigdata3:6378 to bigdata2:6379 Adding replica bigdata1:6378 to bigdata3:6379 M: ffff2d324488baa6612d80d326cc15ce560c5375 bigdata1:6379slots:0-5460 (5461 slots) master S: a4c2b4a75824a58e3b8c4c378da9abbc1bce37db bigdata1:6378replicates 9b2cddbe060c292fb53b0925c46e7d4385a2203d M: 30bc072f6efc950932f63e22a12b2ed3f3da167a bigdata2:6379slots:5461-10922 (5462 slots) master S: 0b90a52a4931dfdd0f50ffb507ba779e32d3446a bigdata2:6378replicates ffff2d324488baa6612d80d326cc15ce560c5375 M: 9b2cddbe060c292fb53b0925c46e7d4385a2203d bigdata3:6379slots:10923-16383 (5461 slots) master S: dd05a7c04115d28b9fc0ebbfbfca59ccdfe556c4 bigdata3:6378replicates 30bc072f6efc950932f63e22a12b2ed3f3da167a Can I set the above configuration? (type 'yes' to accept): yes >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster /usr/local/ruby/ruby-2.3.3/lib/ruby/gems/2.3.0/gems/redis-4.0.1/lib/redis/client.rb:119:in `call': ERR Invalid node address specified: bigdata1:6379 (Redis::CommandError)from /usr/local/ruby/ruby-2.3.3/lib/ruby/gems/2.3.0/gems/redis-4.0.1/lib/redis.rb:2764:in `block in method_missing'from /usr/local/ruby/ruby-2.3.3/lib/ruby/gems/2.3.0/gems/redis-4.0.1/lib/redis.rb:45:in `block in synchronize'from /usr/local/ruby/ruby-2.3.3/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'from /usr/local/ruby/ruby-2.3.3/lib/ruby/gems/2.3.0/gems/redis-4.0.1/lib/redis.rb:45:in `synchronize'from /usr/local/ruby/ruby-2.3.3/lib/ruby/gems/2.3.0/gems/redis-4.0.1/lib/redis.rb:2763:in `method_missing'from ./redis-trib.rb:941:in `block in join_cluster'from ./redis-trib.rb:939:in `each'from ./redis-trib.rb:939:in `join_cluster'from ./redis-trib.rb:1431:in `create_cluster_cmd'from ./redis-trib.rb:1830:in `<main>' [root@bigdata1 src]#

配置完成之后,執(zhí)行下面的命令進(jìn)行查看

[root@bigdata1 redis-4.0.10]# bin/redis-cli -h bigdata1 -p 6379 -a test_redis Warning: Using a password with '-a' option on the command line interface may not be safe. bigdata1:6379> cluster info #查看集群狀態(tài) cluster_state:fail cluster_slots_assigned:5461 cluster_slots_ok:5461 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:1 cluster_size:1 cluster_current_epoch:1 cluster_my_epoch:1 cluster_stats_messages_sent:0 cluster_stats_messages_received:0 bigdata1:6379> cluster nodes #查看節(jié)點(diǎn)信息 5f1f88c2a4869c51ae57d3f4537bf8da19d070f3 xxx.xxx.xxx.142:6378@16378 slave 64a3926e25023c1d391afb88c2850dfdad72657c 0 1530777080000 6 connected 64a3926e25023c1d391afb88c2850dfdad72657c xxx.xxx.xxx.141:6379@16379 master - 0 1530777080000 3 connected 5461-10922 9f0840b083f5223c0c2278b1e75351bc1172d960 xxx.xxx.xxx.142:6379@16379 master - 0 1530777080673 5 connected 10923-16383 5539fdec71b6be6ebc48a36ba4c642a92b27a215 xxx.xxx.xxx.141:6378@16378 slave 7f51d465030d468bcfae748fdf7760f33a469c0c 0 1530777082677 4 connected 7f51d465030d468bcfae748fdf7760f33a469c0c xxx.xxx.xxx.140:6379@16379 myself,master - 0 1530777081000 1 connected 0-5460 34b0cf0efcaefd3f6de8cdbcdb5d0b9b417c836a xxx.xxx.xxx.140:6378@16378 slave 9f0840b083f5223c0c2278b1e75351bc1172d960 0 1530777081675 5 connected bigdata1:6379>

同樣可以使用命令:

[root@bigdata2 src]# ./redis-trib.rb info bigdata1:6379 bigdata1:6379 (ffff2d32...) -> 0 keys | 5461 slots | 0 slaves. [OK] 0 keys in 1 masters. 0.00 keys per slot on average. [root@bigdata2 src]# ./redis-trib.rb info bigdata1:6378 bigdata1:6378 (a4c2b4a7...) -> 0 keys | 0 slots | 0 slaves. [OK] 0 keys in 1 masters. 0.00 keys per slot on average. [root@bigdata2 src]# ./redis-trib.rb info bigdata2:6378 bigdata2:6378 (0b90a52a...) -> 0 keys | 0 slots | 0 slaves. [OK] 0 keys in 1 masters. 0.0 keys per slot on average.

再如:

[root@bigdata2 redis-4.0.10]# bin/redis-cli -h bigdata2 bigdata2:6379> info # Server redis_version:4.0.10 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:8302a44837507475 redis_mode:cluster os:Linux 3.10.0-693.el7.x86_64 x86_64 arch_bits:64 multiplexing_api:epoll atomicvar_api:atomic-builtin gcc_version:4.8.5 process_id:19732 run_id:81a5378421b525b1c97351322949b030c93cc567 tcp_port:6379 uptime_in_seconds:2941 uptime_in_days:0 hz:10 lru_clock:3973023 executable:/usr/local/redis/redis-4.0.10/bin/redis-server config_file:/usr/local/redis/redis-4.0.10/redis.conf# Clients connected_clients:2 client_longest_output_list:0 client_biggest_input_buf:0 blocked_clients:0# Memory used_memory:1529224 used_memory_human:1.46M used_memory_rss:9994240 used_memory_rss_human:9.53M used_memory_peak:1559408 used_memory_peak_human:1.49M used_memory_peak_perc:98.06% used_memory_overhead:1511488 used_memory_startup:1445000 used_memory_dataset:17736 used_memory_dataset_perc:21.06% total_system_memory:16547332096 total_system_memory_human:15.41G used_memory_lua:37888 used_memory_lua_human:37.00K maxmemory:0 maxmemory_human:0B maxmemory_policy:noeviction mem_fragmentation_ratio:6.53 mem_allocator:jemalloc-4.0.3 active_defrag_running:0 lazyfree_pending_objects:0# Persistence loading:0 rdb_changes_since_last_save:0 rdb_bgsave_in_progress:0 rdb_last_save_time:1530696738 rdb_last_bgsave_status:ok rdb_last_bgsave_time_sec:-1 rdb_current_bgsave_time_sec:-1 rdb_last_cow_size:0 aof_enabled:1 aof_rewrite_in_progress:0 aof_rewrite_scheduled:0 aof_last_rewrite_time_sec:-1 aof_current_rewrite_time_sec:-1 aof_last_bgrewrite_status:ok aof_last_write_status:ok aof_last_cow_size:0 aof_current_size:0 aof_base_size:0 aof_pending_rewrite:0 aof_buffer_length:0 aof_rewrite_buffer_length:0 aof_pending_bio_fsync:0 aof_delayed_fsync:0# Stats total_connections_received:4 total_commands_processed:17 instantaneous_ops_per_sec:0 total_net_input_bytes:55966 total_net_output_bytes:30041 instantaneous_input_kbps:0.00 instantaneous_output_kbps:0.00 rejected_connections:0 sync_full:0 sync_partial_ok:0 sync_partial_err:0 expired_keys:0 expired_stale_perc:0.00 expired_time_cap_reached_count:0 evicted_keys:0 keyspace_hits:0 keyspace_misses:0 pubsub_channels:0 pubsub_patterns:0 latest_fork_usec:0 migrate_cached_sockets:0 slave_expires_tracked_keys:0 active_defrag_hits:0 active_defrag_misses:0 active_defrag_key_hits:0 active_defrag_key_misses:0# Replication role:master connected_slaves:0 master_replid:330bc27779b0a28f762a8725d37adff9cde477af master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0# CPU used_cpu_sys:1.13 used_cpu_user:0.78 used_cpu_sys_children:0.00 used_cpu_user_children:0.00# Cluster cluster_enabled:1# Keyspace bigdata2:6379>

在直接使用jedis使用程序連接的時(shí)候,報(bào)如下問(wèn)題:

redis.clients.jedis.exceptions.JedisClusterException:CLUSTERDOWN The cluster is down

解決辦法是:
./redis-trib.rb check xxx.xxx.xxx.142:6379 通過(guò)這種命令檢查各各節(jié)點(diǎn)。發(fā)現(xiàn)會(huì)報(bào)如下問(wèn)題:

[ERR] Not all 16384 slots are covered by nodes

./redis-trib.rb fix xxx.xxx.xxx.142:6379 通過(guò)這個(gè)命令解決上面的問(wèn)題

查看當(dāng)前redis的連接數(shù)量,使用:info clients。

[root@bigdata1 redis-4.0.10-2]# bin/redis-cli -h bigdata1 bigdata1:6379> info clients # Clients connected_clients:3 client_longest_output_list:0 client_biggest_input_buf:0 blocked_clients:0

查看支持的最大連接數(shù)使用config get maxclients

bigdata1:6379> config get maxclients 1) "maxclients" 2) "10000" bigdata1:6379>

關(guān)閉redis的保護(hù)模式:

[root@bigdata1 redis-4.0.10-2]# bin/redis-cli -h bigdata3 -p 6379 -a test_redis [root@bigdata1 redis-4.0.10-2]# bin/redis-cli -h bigdata3 -p 6378 -a test_redis Warning: Using a password with '-a' option on the command line interface may not be safe. bigdata3:6378> config get protected-mode 1) "protected-mode" 2) "yes" bigdata3:6378> config set protected-mode "no" OK

1.3 和Spring繼承

1.3.1 添加maven pom依賴

<aspectjweaver.version>1.8.3</aspectjweaver.version> <redis.clients.version>2.9.0</redis.clients.version><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>${aspectjweaver.version}</version> </dependency> <dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId> </dependency> <dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>${redis.clients.version}</version> </dependency>

1.3.2 編寫Spring的屬性文件

#JedisPoolConfig的參數(shù) #最大連接數(shù),發(fā)現(xiàn)當(dāng)每次最小話300的時(shí)候,在第18次啟動(dòng)的時(shí)候啟動(dòng)不起來(lái)了,連接不夠了。 redis.pool.maxTotal=4000 #最大空閑時(shí)間 redis.pool.maxIdle=300 #最小連接數(shù) redis.pool.minIdle=5 #每次最大連接數(shù) redis.pool.numTestsPerEvictionRun=1024 #釋放掃描的掃描間隔 redis.pool.timeBetweenEvictionRunsMillis=30000 #連接的最小空閑時(shí)間 redis.pool.minEvictableIdleTimeMillis=1800000 #連接控歘按時(shí)間多久后釋放,當(dāng)空閑時(shí)間>該值且空閑連接>最大空閑連接數(shù)時(shí)直接釋放 redis.pool.softMinEvictableIdleTimeMillis=10000 #獲得鏈接時(shí)的最大等待毫秒數(shù),小于0:阻塞不確定時(shí)間,默認(rèn)-1 redis.pool.maxWaitMillis=3000 #在獲得鏈接的時(shí)候檢查有效性,默認(rèn)false redis.pool.testOnBorrow=true #在空閑時(shí)檢查有效性,默認(rèn)false redis.pool.testWhileIdle=true #連接耗盡時(shí)是否阻塞,false報(bào)異常,true阻塞超時(shí),默認(rèn)true redis.pool.blockWhenExhausted=false #RedisClusterConfiguration配置 redis.maxRedirects=5#主機(jī)和端口號(hào) redis.host1=xxx.xxx.xxx.140 redis.port1=6379redis.host2=xxx.xxx.xxx.140 redis.port2=6378redis.host3=xxx.xxx.xxx.141 redis.port3=6379redis.host4=xxx.xxx.xxx.141 redis.port4=6378redis.host5=xxx.xxx.xxx.142 redis.port5=6379redis.host6=xxx.xxx.xxx.142 redis.port6=6378redis.password=test_redis redis.dataBase=0#單redis節(jié)點(diǎn) redis.host=xxx.xxx.xxx.141 redis.port=6377

1.3.2 添加Spring的配置文件

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--配置redis--><!-- redis 相關(guān)配置 --><bean id="baseRedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"><!-- 最大連接數(shù) --><property name="maxTotal" value="${redis.pool.maxTotal}" /><!-- 最大空閑時(shí)間 --><property name="maxIdle" value="${redis.pool.maxIdle}" /><!-- 最小連接數(shù) --><property name="minIdle" value="${redis.pool.minIdle}" /><!-- 每次最大連接數(shù) --><property name="numTestsPerEvictionRun" value="${redis.pool.numTestsPerEvictionRun}" /><!-- 釋放掃描的掃描間隔 --><property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}" /><!-- 連接的最小空閑時(shí)間 --><property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}" /><!-- 連接控歘按時(shí)間多久后釋放,當(dāng)空閑時(shí)間>該值且空閑連接>最大空閑連接數(shù)時(shí)直接釋放 --><property name="softMinEvictableIdleTimeMillis" value="${redis.pool.softMinEvictableIdleTimeMillis}" /><!-- 獲得鏈接時(shí)的最大等待毫秒數(shù),小于0:阻塞不確定時(shí)間,默認(rèn)-1 --><property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" /><!-- 在獲得鏈接的時(shí)候檢查有效性,默認(rèn)false --><property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /><!-- 在空閑時(shí)檢查有效性,默認(rèn)false --><property name="testWhileIdle" value="${redis.pool.testWhileIdle}" /><!-- 連接耗盡時(shí)是否阻塞,false報(bào)異常,true阻塞超時(shí),默認(rèn)true --><property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}" /></bean><!--配置RedisClusterConfiguration--><bean id="baseRedisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration"><property name="maxRedirects" value="${redis.maxRedirects}" /><property name="clusterNodes"><set><bean class="org.springframework.data.redis.connection.RedisClusterNode"><constructor-arg name="host" value="${redis.host1}" /><constructor-arg name="port" value="${redis.port1}"/></bean><bean class="org.springframework.data.redis.connection.RedisClusterNode"><constructor-arg name="host" value="${redis.host2}" /><constructor-arg name="port" value="${redis.port2}" /></bean><bean class="org.springframework.data.redis.connection.RedisClusterNode"><constructor-arg name="host" value="${redis.host3}" /><constructor-arg name="port" value="${redis.port3}" /></bean><bean class="org.springframework.data.redis.connection.RedisClusterNode"><constructor-arg name="host" value="${redis.host4}" /><constructor-arg name="port" value="${redis.port4}" /></bean><bean class="org.springframework.data.redis.connection.RedisClusterNode"><constructor-arg name="host" value="${redis.host5}" /><constructor-arg name="port" value="${redis.port5}" /></bean><bean class="org.springframework.data.redis.connection.RedisClusterNode"><constructor-arg name="host" value="${redis.host6}" /><constructor-arg name="port" value="${redis.port6}" /></bean></set></property></bean><!-- 單個(gè)redis場(chǎng)景設(shè)置,單集的時(shí)候放開下面的代碼 --><bean id="baseJedisConnFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy"><property name="hostName" value="${redis.host}" /><property name="port" value="${redis.port}" /><property name="password" value ="${redis.password}" /><property name="usePool" value="true" /><property name="poolConfig" ref="baseRedisPoolConfig" /></bean><!--集群場(chǎng)景下的配置,集群的時(shí)候,放開下面的配置--><!--<bean id="baseJedisConnFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy"><constructor-arg name="poolConfig" ref="baseRedisPoolConfig" /><constructor-arg name="clusterConfig" ref="baseRedisClusterConfiguration" />&lt;!&ndash;<property name="password" value ="${redis.password}" />&ndash;&gt;<property name="usePool" value="true" /><property name="database" value="${redis.dataBase}" /></bean>--><!-- redis template definition --><bean id="baseRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate"><property name="connectionFactory" ref="baseJedisConnFactory" /><property name="keySerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /></property><property name="valueSerializer"><bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /></property><property name="hashKeySerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /></property><property name="hashValueSerializer"><bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /></property></bean><bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"><property name="connectionFactory" ref="baseJedisConnFactory" /></bean><bean id="redisUtil" class="com.xxx.redis.util.RedisUtil"><property name="stringRedisTemplate" ref="stringRedisTemplate" /><property name="redisTemplate" ref="baseRedisTemplate"/></bean> </beans>

1.4 示例redis操作的代碼

package com.xxx.redis.util;import org.springframework.data.redis.connection.DataType; import org.springframework.data.redis.core.*; import org.springframework.data.redis.core.ZSetOperations.TypedTuple;import java.io.Serializable; import java.util.*; import java.util.Map.Entry; import java.util.concurrent.TimeUnit;/*** Redis工具類** 參考地址:http://hbxflihua.iteye.com/blog/2328156* (GitHub文檔: https://github.com/whvcse/RedisUtil )** @author WangFan* @date 2018-02-24 下午03:09:50* @version 1.1 (GitHub文檔: https://github.com/whvcse/RedisUtil )*/ public class RedisUtil {private StringRedisTemplate stringRedisTemplate;private RedisTemplate<Serializable, Object> redisTemplate;public StringRedisTemplate getStringRedisTemplate() {return stringRedisTemplate;}public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {this.stringRedisTemplate = stringRedisTemplate;}public RedisTemplate<Serializable, Object> getRedisTemplate() {return redisTemplate;}public void setRedisTemplate(RedisTemplate<Serializable, Object> redisTemplate) {this.redisTemplate = redisTemplate;}/** -------------------key相關(guān)操作--------------------- *//*** 刪除key** @param key*/public void delete(String key) {stringRedisTemplate.delete(key);}/*** 批量刪除key** @param keys*/public void delete(Collection<String> keys) {stringRedisTemplate.delete(keys);}/*** 序列化key** @param key* @return*/public byte[] dump(String key) {return stringRedisTemplate.dump(key);}/*** 是否存在key** @param key* @return*/public Boolean hasKey(String key) {return stringRedisTemplate.hasKey(key);}/*** 設(shè)置過(guò)期時(shí)間** @param key* @param timeout* @param unit* @return*/public Boolean expire(String key, long timeout, TimeUnit unit) {return stringRedisTemplate.expire(key, timeout, unit);}/*** 設(shè)置過(guò)期時(shí)間** @param key* @param date* @return*/public Boolean expireAt(String key, Date date) {return stringRedisTemplate.expireAt(key, date);}/*** 查找匹配的key** @param pattern* @return*/public Set<String> keys(String pattern) {return stringRedisTemplate.keys(pattern);}/*** 將當(dāng)前數(shù)據(jù)庫(kù)的 key 移動(dòng)到給定的數(shù)據(jù)庫(kù) db 當(dāng)中** @param key* @param dbIndex* @return*/public Boolean move(String key, int dbIndex) {return stringRedisTemplate.move(key, dbIndex);}/*** 移除 key 的過(guò)期時(shí)間,key 將持久保持** @param key* @return*/public Boolean persist(String key) {return stringRedisTemplate.persist(key);}/*** 返回 key 的剩余的過(guò)期時(shí)間** @param key* @param unit* @return*/public Long getExpire(String key, TimeUnit unit) {return stringRedisTemplate.getExpire(key, unit);}/*** 返回 key 的剩余的過(guò)期時(shí)間** @param key* @return*/public Long getExpire(String key) {return stringRedisTemplate.getExpire(key);}/*** 從當(dāng)前數(shù)據(jù)庫(kù)中隨機(jī)返回一個(gè) key** @return*/public String randomKey() {return stringRedisTemplate.randomKey();}/*** 修改 key 的名稱** @param oldKey* @param newKey*/public void rename(String oldKey, String newKey) {stringRedisTemplate.rename(oldKey, newKey);}/*** 僅當(dāng) newkey 不存在時(shí),將 oldKey 改名為 newkey** @param oldKey* @param newKey* @return*/public Boolean renameIfAbsent(String oldKey, String newKey) {return stringRedisTemplate.renameIfAbsent(oldKey, newKey);}/*** 返回 key 所儲(chǔ)存的值的類型** @param key* @return*/public DataType type(String key) {return stringRedisTemplate.type(key);}/** -------------------string相關(guān)操作--------------------- *//*** 設(shè)置指定 key 的值* @param key* @param value*/public void setString(String key, String value) {stringRedisTemplate.opsForValue().set(key, value);}/*** 獲取指定 key 的值* @param key* @return*/public String getString(String key) {return stringRedisTemplate.opsForValue().get(key);}/*** 返回 key 中字符串值的子字符* @param key* @param start* @param end* @return*/public String getRange(String key, long start, long end) {return stringRedisTemplate.opsForValue().get(key, start, end);}/*** 將給定 key 的值設(shè)為 value ,并返回 key 的舊值(old value)** @param key* @param value* @return*/public String getAndSet(String key, String value) {return stringRedisTemplate.opsForValue().getAndSet(key, value);}/*** 對(duì) key 所儲(chǔ)存的字符串值,獲取指定偏移量上的位(bit)** @param key* @param offset* @return*/public Boolean getBit(String key, long offset) {return stringRedisTemplate.opsForValue().getBit(key, offset);}/*** 批量獲取** @param keys* @return*/public List<String> multiGet(Collection<String> keys) {return stringRedisTemplate.opsForValue().multiGet(keys);}/*** 設(shè)置ASCII碼, 字符串'a'的ASCII碼是97, 轉(zhuǎn)為二進(jìn)制是'01100001', 此方法是將二進(jìn)制第offset位值變?yōu)関alue** @param key* @param offset* 位置* @param value* 值,true為1, false為0* @return*/public boolean setBit(String key, long offset, boolean value) {return stringRedisTemplate.opsForValue().setBit(key, offset, value);}/*** 將值 value 關(guān)聯(lián)到 key ,并將 key 的過(guò)期時(shí)間設(shè)為 timeout** @param key* @param value* @param timeout* 過(guò)期時(shí)間* @param unit* 時(shí)間單位, 天:TimeUnit.DAYS 小時(shí):TimeUnit.HOURS 分鐘:TimeUnit.MINUTES* 秒:TimeUnit.SECONDS 毫秒:TimeUnit.MILLISECONDS*/public void setEx(String key, String value, long timeout, TimeUnit unit) {stringRedisTemplate.opsForValue().set(key, value, timeout, unit);}/*** 只有在 key 不存在時(shí)設(shè)置 key 的值** @param key* @param value* @return 之前已經(jīng)存在返回false,不存在返回true*/public boolean setIfAbsent(String key, String value) {return stringRedisTemplate.opsForValue().setIfAbsent(key, value);}/*** 用 value 參數(shù)覆寫給定 key 所儲(chǔ)存的字符串值,從偏移量 offset 開始** @param key* @param value* @param offset* 從指定位置開始覆寫*/public void setRange(String key, String value, long offset) {stringRedisTemplate.opsForValue().set(key, value, offset);}/*** 獲取字符串的長(zhǎng)度** @param key* @return*/public Long size(String key) {return stringRedisTemplate.opsForValue().size(key);}/*** 批量添加** @param maps*/public void multiSet(Map<String, String> maps) {stringRedisTemplate.opsForValue().multiSet(maps);}/*** 同時(shí)設(shè)置一個(gè)或多個(gè) key-value 對(duì),當(dāng)且僅當(dāng)所有給定 key 都不存在** @param maps* @return 之前已經(jīng)存在返回false,不存在返回true*/public boolean multiSetIfAbsent(Map<String, String> maps) {return stringRedisTemplate.opsForValue().multiSetIfAbsent(maps);}/*** 增加(自增長(zhǎng)), 負(fù)數(shù)則為自減** @param key* @param increment* @return*/public Long incrBy(String key, long increment) {return stringRedisTemplate.opsForValue().increment(key, increment);}/**** @param key* @param increment* @return*/public Double incrByFloat(String key, double increment) {return stringRedisTemplate.opsForValue().increment(key, increment);}/*** 追加到末尾** @param key* @param value* @return*/public Integer append(String key, String value) {return stringRedisTemplate.opsForValue().append(key, value);}/** -------------------hash相關(guān)操作------------------------- *//*** 獲取存儲(chǔ)在哈希表中指定字段的值** @param key* @param field* @return*/public Object hGet(String key, String field) {return stringRedisTemplate.opsForHash().get(key, field);}/*** 獲取所有給定字段的值** @param key* @return*/public Map<Object, Object> hGetAll(String key) {return stringRedisTemplate.opsForHash().entries(key);}/*** 獲取所有給定字段的值** @param key* @param fields* @return*/public List<Object> hMultiGet(String key, Collection<Object> fields) {return stringRedisTemplate.opsForHash().multiGet(key, fields);}public void hPut(String key, String hashKey, String value) {stringRedisTemplate.opsForHash().put(key, hashKey, value);}public void hPutAll(String key, Map<String, String> maps) {stringRedisTemplate.opsForHash().putAll(key, maps);}/*** 僅當(dāng)hashKey不存在時(shí)才設(shè)置** @param key* @param hashKey* @param value* @return*/public Boolean hPutIfAbsent(String key, String hashKey, String value) {return stringRedisTemplate.opsForHash().putIfAbsent(key, hashKey, value);}/*** 刪除一個(gè)或多個(gè)哈希表字段** @param key* @param fields* @return*/public Long hDelete(String key, Object... fields) {return stringRedisTemplate.opsForHash().delete(key, fields);}/*** 查看哈希表 key 中,指定的字段是否存在** @param key* @param field* @return*/public boolean hExists(String key, String field) {return stringRedisTemplate.opsForHash().hasKey(key, field);}/*** 為哈希表 key 中的指定字段的整數(shù)值加上增量 increment** @param key* @param field* @param increment* @return*/public Long hIncrBy(String key, Object field, long increment) {return stringRedisTemplate.opsForHash().increment(key, field, increment);}/*** 為哈希表 key 中的指定字段的整數(shù)值加上增量 increment** @param key* @param field* @param delta* @return*/public Double hIncrByFloat(String key, Object field, double delta) {return stringRedisTemplate.opsForHash().increment(key, field, delta);}/*** 獲取所有哈希表中的字段** @param key* @return*/public Set<Object> hKeys(String key) {return stringRedisTemplate.opsForHash().keys(key);}/*** 獲取哈希表中字段的數(shù)量** @param key* @return*/public Long hSize(String key) {return stringRedisTemplate.opsForHash().size(key);}/*** 獲取哈希表中所有值** @param key* @return*/public List<Object> hValues(String key) {return stringRedisTemplate.opsForHash().values(key);}/*** 迭代哈希表中的鍵值對(duì)** @param key* @param options* @return*/public Cursor<Entry<Object, Object>> hScan(String key, ScanOptions options) {return stringRedisTemplate.opsForHash().scan(key, options);}/** ------------------------list相關(guān)操作---------------------------- *//*** 通過(guò)索引獲取列表中的元素** @param key* @param index* @return*/public String lIndex(String key, long index) {return stringRedisTemplate.opsForList().index(key, index);}/*** 獲取列表指定范圍內(nèi)的元素** @param key* @param start* 開始位置, 0是開始位置* @param end* 結(jié)束位置, -1返回所有* @return*/public List<String> lRange(String key, long start, long end) {return stringRedisTemplate.opsForList().range(key, start, end);}/*** 存儲(chǔ)在list頭部** @param key* @param value* @return*/public Long lLeftPush(String key, String value) {return stringRedisTemplate.opsForList().leftPush(key, value);}/**** @param key* @param value* @return*/public Long lLeftPushAll(String key, String... value) {return stringRedisTemplate.opsForList().leftPushAll(key, value);}/**** @param key* @param value* @return*/public Long lLeftPushAll(String key, Collection<String> value) {return stringRedisTemplate.opsForList().leftPushAll(key, value);}/*** 當(dāng)list存在的時(shí)候才加入** @param key* @param value* @return*/public Long lLeftPushIfPresent(String key, String value) {return stringRedisTemplate.opsForList().leftPushIfPresent(key, value);}/*** 如果pivot存在,再pivot前面添加** @param key* @param pivot* @param value* @return*/public Long lLeftPush(String key, String pivot, String value) {return stringRedisTemplate.opsForList().leftPush(key, pivot, value);}/**** @param key* @param value* @return*/public Long lRightPush(String key, String value) {return stringRedisTemplate.opsForList().rightPush(key, value);}/**** @param key* @param value* @return*/public Long lRightPushAll(String key, String... value) {return stringRedisTemplate.opsForList().rightPushAll(key, value);}/**** @param key* @param value* @return*/public Long lRightPushAll(String key, Collection<String> value) {return stringRedisTemplate.opsForList().rightPushAll(key, value);}/*** 為已存在的列表添加值** @param key* @param value* @return*/public Long lRightPushIfPresent(String key, String value) {return stringRedisTemplate.opsForList().rightPushIfPresent(key, value);}/*** 在pivot元素的右邊添加值** @param key* @param pivot* @param value* @return*/public Long lRightPush(String key, String pivot, String value) {return stringRedisTemplate.opsForList().rightPush(key, pivot, value);}/*** 通過(guò)索引設(shè)置列表元素的值** @param key* @param index* 位置* @param value*/public void lSet(String key, long index, String value) {stringRedisTemplate.opsForList().set(key, index, value);}/*** 移出并獲取列表的第一個(gè)元素** @param key* @return 刪除的元素*/public String lLeftPop(String key) {return stringRedisTemplate.opsForList().leftPop(key);}/*** 移出并獲取列表的第一個(gè)元素, 如果列表沒(méi)有元素會(huì)阻塞列表直到等待超時(shí)或發(fā)現(xiàn)可彈出元素為止** @param key* @param timeout* 等待時(shí)間* @param unit* 時(shí)間單位* @return*/public String lBLeftPop(String key, long timeout, TimeUnit unit) {return stringRedisTemplate.opsForList().leftPop(key, timeout, unit);}/*** 移除并獲取列表最后一個(gè)元素** @param key* @return 刪除的元素*/public String lRightPop(String key) {return stringRedisTemplate.opsForList().rightPop(key);}/*** 移出并獲取列表的最后一個(gè)元素, 如果列表沒(méi)有元素會(huì)阻塞列表直到等待超時(shí)或發(fā)現(xiàn)可彈出元素為止** @param key* @param timeout* 等待時(shí)間* @param unit* 時(shí)間單位* @return*/public String lBRightPop(String key, long timeout, TimeUnit unit) {return stringRedisTemplate.opsForList().rightPop(key, timeout, unit);}/*** 移除列表的最后一個(gè)元素,并將該元素添加到另一個(gè)列表并返回** @param sourceKey* @param destinationKey* @return*/public String lRightPopAndLeftPush(String sourceKey, String destinationKey) {return stringRedisTemplate.opsForList().rightPopAndLeftPush(sourceKey,destinationKey);}/*** 從列表中彈出一個(gè)值,將彈出的元素插入到另外一個(gè)列表中并返回它; 如果列表沒(méi)有元素會(huì)阻塞列表直到等待超時(shí)或發(fā)現(xiàn)可彈出元素為止** @param sourceKey* @param destinationKey* @param timeout* @param unit* @return*/public String lBRightPopAndLeftPush(String sourceKey, String destinationKey,long timeout, TimeUnit unit) {return stringRedisTemplate.opsForList().rightPopAndLeftPush(sourceKey,destinationKey, timeout, unit);}/*** 刪除集合中值等于value得元素** @param key* @param index* index=0, 刪除所有值等于value的元素; index>0, 從頭部開始刪除第一個(gè)值等于value的元素;* index<0, 從尾部開始刪除第一個(gè)值等于value的元素;* @param value* @return*/public Long lRemove(String key, long index, String value) {return stringRedisTemplate.opsForList().remove(key, index, value);}/*** 裁剪list** @param key* @param start* @param end*/public void lTrim(String key, long start, long end) {stringRedisTemplate.opsForList().trim(key, start, end);}/*** 獲取列表長(zhǎng)度** @param key* @return*/public Long lLen(String key) {return stringRedisTemplate.opsForList().size(key);}/** --------------------set相關(guān)操作-------------------------- *//*** set添加元素** @param key* @param values* @return*/public Long sAdd(String key, String... values) {return stringRedisTemplate.opsForSet().add(key, values);}/*** set移除元素** @param key* @param values* @return*/public Long sRemove(String key, Object... values) {return stringRedisTemplate.opsForSet().remove(key, values);}/*** 移除并返回集合的一個(gè)隨機(jī)元素** @param key* @return*/public String sPop(String key) {return stringRedisTemplate.opsForSet().pop(key);}/*** 將元素value從一個(gè)集合移到另一個(gè)集合** @param key* @param value* @param destKey* @return*/public Boolean sMove(String key, String value, String destKey) {return stringRedisTemplate.opsForSet().move(key, value, destKey);}/*** 獲取集合的大小** @param key* @return*/public Long sSize(String key) {return stringRedisTemplate.opsForSet().size(key);}/*** 判斷集合是否包含value** @param key* @param value* @return*/public Boolean sIsMember(String key, Object value) {return stringRedisTemplate.opsForSet().isMember(key, value);}/*** 獲取兩個(gè)集合的交集** @param key* @param otherKey* @return*/public Set<String> sIntersect(String key, String otherKey) {return stringRedisTemplate.opsForSet().intersect(key, otherKey);}/*** 獲取key集合與多個(gè)集合的交集** @param key* @param otherKeys* @return*/public Set<String> sIntersect(String key, Collection<String> otherKeys) {return stringRedisTemplate.opsForSet().intersect(key, otherKeys);}/*** key集合與otherKey集合的交集存儲(chǔ)到destKey集合中** @param key* @param otherKey* @param destKey* @return*/public Long sIntersectAndStore(String key, String otherKey, String destKey) {return stringRedisTemplate.opsForSet().intersectAndStore(key, otherKey,destKey);}/*** key集合與多個(gè)集合的交集存儲(chǔ)到destKey集合中** @param key* @param otherKeys* @param destKey* @return*/public Long sIntersectAndStore(String key, Collection<String> otherKeys,String destKey) {return stringRedisTemplate.opsForSet().intersectAndStore(key, otherKeys,destKey);}/*** 獲取兩個(gè)集合的并集** @param key* @param otherKeys* @return*/public Set<String> sUnion(String key, String otherKeys) {return stringRedisTemplate.opsForSet().union(key, otherKeys);}/*** 獲取key集合與多個(gè)集合的并集** @param key* @param otherKeys* @return*/public Set<String> sUnion(String key, Collection<String> otherKeys) {return stringRedisTemplate.opsForSet().union(key, otherKeys);}/*** key集合與otherKey集合的并集存儲(chǔ)到destKey中** @param key* @param otherKey* @param destKey* @return*/public Long sUnionAndStore(String key, String otherKey, String destKey) {return stringRedisTemplate.opsForSet().unionAndStore(key, otherKey, destKey);}/*** key集合與多個(gè)集合的并集存儲(chǔ)到destKey中** @param key* @param otherKeys* @param destKey* @return*/public Long sUnionAndStore(String key, Collection<String> otherKeys,String destKey) {return stringRedisTemplate.opsForSet().unionAndStore(key, otherKeys, destKey);}/*** 獲取兩個(gè)集合的差集** @param key* @param otherKey* @return*/public Set<String> sDifference(String key, String otherKey) {return stringRedisTemplate.opsForSet().difference(key, otherKey);}/*** 獲取key集合與多個(gè)集合的差集** @param key* @param otherKeys* @return*/public Set<String> sDifference(String key, Collection<String> otherKeys) {return stringRedisTemplate.opsForSet().difference(key, otherKeys);}/*** key集合與otherKey集合的差集存儲(chǔ)到destKey中** @param key* @param otherKey* @param destKey* @return*/public Long sDifference(String key, String otherKey, String destKey) {return stringRedisTemplate.opsForSet().differenceAndStore(key, otherKey,destKey);}/*** key集合與多個(gè)集合的差集存儲(chǔ)到destKey中** @param key* @param otherKeys* @param destKey* @return*/public Long sDifference(String key, Collection<String> otherKeys,String destKey) {return stringRedisTemplate.opsForSet().differenceAndStore(key, otherKeys,destKey);}/*** 獲取集合所有元素** @param key* @return*/public Set<String> setMembers(String key) {return stringRedisTemplate.opsForSet().members(key);}/*** 隨機(jī)獲取集合中的一個(gè)元素** @param key* @return*/public String sRandomMember(String key) {return stringRedisTemplate.opsForSet().randomMember(key);}/*** 隨機(jī)獲取集合中count個(gè)元素** @param key* @param count* @return*/public List<String> sRandomMembers(String key, long count) {return stringRedisTemplate.opsForSet().randomMembers(key, count);}/*** 隨機(jī)獲取集合中count個(gè)元素并且去除重復(fù)的** @param key* @param count* @return*/public Set<String> sDistinctRandomMembers(String key, long count) {return stringRedisTemplate.opsForSet().distinctRandomMembers(key, count);}/**** @param key* @param options* @return*/public Cursor<String> sScan(String key, ScanOptions options) {return stringRedisTemplate.opsForSet().scan(key, options);}/**------------------zSet相關(guān)操作--------------------------------*//*** 添加元素,有序集合是按照元素的score值由小到大排列** @param key* @param value* @param score* @return*/public Boolean zAdd(String key, String value, double score) {return stringRedisTemplate.opsForZSet().add(key, value, score);}/**** @param key* @param values* @return*/public Long zAdd(String key, Set<TypedTuple<String>> values) {return stringRedisTemplate.opsForZSet().add(key, values);}/**** @param key* @param values* @return*/public Long zRemove(String key, Object... values) {return stringRedisTemplate.opsForZSet().remove(key, values);}/*** 增加元素的score值,并返回增加后的值** @param key* @param value* @param delta* @return*/public Double zIncrementScore(String key, String value, double delta) {return stringRedisTemplate.opsForZSet().incrementScore(key, value, delta);}/*** 返回元素在集合的排名,有序集合是按照元素的score值由小到大排列** @param key* @param value* @return 0表示第一位*/public Long zRank(String key, Object value) {return stringRedisTemplate.opsForZSet().rank(key, value);}/*** 返回元素在集合的排名,按元素的score值由大到小排列** @param key* @param value* @return*/public Long zReverseRank(String key, Object value) {return stringRedisTemplate.opsForZSet().reverseRank(key, value);}/*** 獲取集合的元素, 從小到大排序** @param key* @param start* 開始位置* @param end* 結(jié)束位置, -1查詢所有* @return*/public Set<String> zRange(String key, long start, long end) {return stringRedisTemplate.opsForZSet().range(key, start, end);}/*** 獲取集合元素, 并且把score值也獲取** @param key* @param start* @param end* @return*/public Set<TypedTuple<String>> zRangeWithScores(String key, long start,long end) {return stringRedisTemplate.opsForZSet().rangeWithScores(key, start, end);}/*** 根據(jù)Score值查詢集合元素** @param key* @param min* 最小值* @param max* 最大值* @return*/public Set<String> zRangeByScore(String key, double min, double max) {return stringRedisTemplate.opsForZSet().rangeByScore(key, min, max);}/*** 根據(jù)Score值查詢集合元素, 從小到大排序** @param key* @param min* 最小值* @param max* 最大值* @return*/public Set<TypedTuple<String>> zRangeByScoreWithScores(String key,double min, double max) {return stringRedisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max);}/**** @param key* @param min* @param max* @param start* @param end* @return*/public Set<TypedTuple<String>> zRangeByScoreWithScores(String key,double min, double max, long start, long end) {return stringRedisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max,start, end);}/*** 獲取集合的元素, 從大到小排序** @param key* @param start* @param end* @return*/public Set<String> zReverseRange(String key, long start, long end) {return stringRedisTemplate.opsForZSet().reverseRange(key, start, end);}/*** 獲取集合的元素, 從大到小排序, 并返回score值** @param key* @param start* @param end* @return*/public Set<TypedTuple<String>> zReverseRangeWithScores(String key,long start, long end) {return stringRedisTemplate.opsForZSet().reverseRangeWithScores(key, start,end);}/*** 根據(jù)Score值查詢集合元素, 從大到小排序** @param key* @param min* @param max* @return*/public Set<String> zReverseRangeByScore(String key, double min,double max) {return stringRedisTemplate.opsForZSet().reverseRangeByScore(key, min, max);}/*** 根據(jù)Score值查詢集合元素, 從大到小排序** @param key* @param min* @param max* @return*/public Set<TypedTuple<String>> zReverseRangeByScoreWithScores(String key, double min, double max) {return stringRedisTemplate.opsForZSet().reverseRangeByScoreWithScores(key,min, max);}/**** @param key* @param min* @param max* @param start* @param end* @return*/public Set<String> zReverseRangeByScore(String key, double min,double max, long start, long end) {return stringRedisTemplate.opsForZSet().reverseRangeByScore(key, min, max,start, end);}/*** 根據(jù)score值獲取集合元素?cái)?shù)量** @param key* @param min* @param max* @return*/public Long zCount(String key, double min, double max) {return stringRedisTemplate.opsForZSet().count(key, min, max);}/*** 獲取集合大小** @param key* @return*/public Long zSize(String key) {return stringRedisTemplate.opsForZSet().size(key);}/*** 獲取集合大小** @param key* @return*/public Long zZCard(String key) {return stringRedisTemplate.opsForZSet().zCard(key);}/*** 獲取集合中value元素的score值** @param key* @param value* @return*/public Double zScore(String key, Object value) {return stringRedisTemplate.opsForZSet().score(key, value);}/*** 移除指定索引位置的成員** @param key* @param start* @param end* @return*/public Long zRemoveRange(String key, long start, long end) {return stringRedisTemplate.opsForZSet().removeRange(key, start, end);}/*** 根據(jù)指定的score值的范圍來(lái)移除成員** @param key* @param min* @param max* @return*/public Long zRemoveRangeByScore(String key, double min, double max) {return stringRedisTemplate.opsForZSet().removeRangeByScore(key, min, max);}/*** 獲取key和otherKey的并集并存儲(chǔ)在destKey中** @param key* @param otherKey* @param destKey* @return*/public Long zUnionAndStore(String key, String otherKey, String destKey) {return stringRedisTemplate.opsForZSet().unionAndStore(key, otherKey, destKey);}/**** @param key* @param otherKeys* @param destKey* @return*/public Long zUnionAndStore(String key, Collection<String> otherKeys,String destKey) {return stringRedisTemplate.opsForZSet().unionAndStore(key, otherKeys, destKey);}/*** 交集** @param key* @param otherKey* @param destKey* @return*/public Long zIntersectAndStore(String key, String otherKey,String destKey) {return stringRedisTemplate.opsForZSet().intersectAndStore(key, otherKey,destKey);}/*** 交集** @param key* @param otherKeys* @param destKey* @return*/public Long zIntersectAndStore(String key, Collection<String> otherKeys,String destKey) {return stringRedisTemplate.opsForZSet().intersectAndStore(key, otherKeys,destKey);}/**** @param key* @param options* @return*/public Cursor<TypedTuple<String>> zScan(String key, ScanOptions options) {return stringRedisTemplate.opsForZSet().scan(key, options);}/*** 存儲(chǔ)map值 : 參考文檔:https://www.jianshu.com/p/7bf5dc61ca06* @param key :表示存儲(chǔ)map的key* @param mapData :存儲(chǔ)的map值* @param timeout :超時(shí)時(shí)間,-1:表示用不過(guò)期* @param unit :時(shí)間單位*/public void setMap(String key, Map<Object,Object> mapData, Long timeout, TimeUnit unit) {//下面是方案一//for (Map.Entry<String,Object> entry : mapData.entrySet()) {// redisTemplate.opsForHash().put(key,entry.getKey(),entry.getValue());//}redisTemplate.opsForHash().putAll(key,mapData);if (timeout.longValue() >= 0L) {redisTemplate.expire(key, timeout.longValue(), unit);}}/*** 往redis 中key的map中添加 (mapKey,mapValue)* 參考網(wǎng)址:https://www.jianshu.com/p/7bf5dc61ca06** @param key :redis中代表map的key值* @param mapKey :map中key為mapKey* @param mapValue :map中的value值為mapValue*/public void putMapOneKV(String key,String mapKey,Object mapValue) {redisTemplate.opsForHash().put(key,mapKey,mapValue);}/*** 刪除redis 為key的map中的hashKeys這些值* @param key* @param mapKyes* @return*/public void deleteMapOneK(String key,String... mapKyes) {for (String hashKey : mapKyes) {redisTemplate.opsForHash().delete(key,hashKey);}}/*** 刪除map值* @param key*/public void deleteMap(String key) {//設(shè)置key過(guò)期redisTemplate.expire(key, 0, TimeUnit.MILLISECONDS);}/*** 獲取到redis 中key的map對(duì)象* @param key :reids中的key值* @return*/public Map<Object,Object> getMap(String key) {return redisTemplate.opsForHash().entries(key);}/*** 獲取到redis中存儲(chǔ)的map的值的數(shù)量* @param key* @return*/public Long getMapSize(String key) {return redisTemplate.opsForHash().size(key);}/*** 讀取緩存,此種場(chǎng)景存值的時(shí)候使用的是setObject** @param key* @return*/public Object getObject(final String key) {Object result = null;ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();result = operations.get(key);return result;}/*** 存值方法,如果存的時(shí)候使用setObject方法,取參的時(shí)候請(qǐng)用getObject方法* @param key* @param value* @return*/public Object setObject(final String key, Object value) {boolean result = false;try {ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();operations.set(key, value);result = true;} catch (Exception e) {e.printStackTrace();}return result;}}

總結(jié)

以上是生活随笔為你收集整理的redis-4.0.10集群安装(3台机器,6个node),以及在Spring项目中的集成,redis操作工具类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。