Redis 是最流行的 NoSQL 數據庫解決方案之一,而 Java 是世界上最流行(注意,我沒有說“最好”)的編程語言之一。雖然兩者看起來很自然地在一起“工作”,但是要知道,Redis 其實並沒有對 Java 提供原生支持。
相反,作為 Java 開發人員,我們若想在程序中集成 Redis,必須使用 Redis 的第三方庫。而 Redisson 就是用於在 Java 程序中操作 Redis 的庫,它使得我們可以在程序中輕松地使用 Redis。Redisson 在 java.util
中常用接口的基礎上,為我們提供了一系列具有分布式特性的工具類。
使用 RList 操作 Redis中的列表(List)

import org.redisson.Redisson; import org.redisson.api.RList; import org.redisson.api.RedissonClient; public class ListExamples { public static void main(String[] args) { // 默認連接上 127.0.0.1:6379 RedissonClient client = Redisson.create(); // RList 繼承了 java.util.List 接口 RList<String> nameList = client.getList("nameList"); nameList.clear(); nameList.add("bingo"); nameList.add("yanglbme"); nameList.add("https://github.com/yanglbme"); nameList.remove(-1); boolean contains = nameList.contains("yanglbme"); System.out.println("List size: " + nameList.size()); System.out.println("Is list contains name 'yanglbme': " + contains); nameList.forEach(System.out::println); client.shutdown(); } }
運行上面的代碼時,可以獲得以下輸出:
使用 RMap 操作 Redis 哈希

public class MapExamples { public static void main(String[] args) { // 默認連接上127.0.0.1:6379 RedissonClient client = Redisson.create(); // RMap 繼承了 java.util.concurrent.ConcurrentMap 接口 RMap<String, String> map = client.getMap("personalInfo"); map.put("name", "yanglbme"); map.put("address", "Shenzhen"); map.put("link", "https://github.com/yanglbme"); boolean contains = map.containsKey("link"); System.out.println("Map size: " + map.size()); System.out.println("Is map contains key 'link': " + contains); String value = map.get("name"); System.out.println("Value mapped by key 'name': " + value); boolean added = map.putIfAbsent("link", "https://doocs.github.io") == null; System.out.println("Is value mapped by key 'link' added: " + added); client.shutdown(); } }
使用 RLock 實現 Redis 分布式鎖
RLock 是 Java 中可重入鎖的分布式實現,下面的代碼演示了 RLock 的用法:

import org.redisson.Redisson; import org.redisson.api.RLock; import org.redisson.api.RedissonClient; public class LockExamples { public static void main(String[] args) throws InterruptedException { // 默認連接上127.0.0.1:6379 RedissonClient client = Redisson.create(); // RLock 繼承了 java.util.concurrent.locks.Lock 接口 RLock lock = client.getLock("lock"); lock.lock(); System.out.println("lock acquired"); Thread t = new Thread(() -> { RLock lock1 = client.getLock("lock"); lock1.lock(); System.out.println("lock acquired by thread"); lock1.unlock(); System.out.println("lock released by thread"); }); t.start(); t.join(1000); lock.unlock(); System.out.println("lock released"); t.join(); client.shutdown(); } }
使用 RAtomicLong 實現 Redis 原子操作
RAtomicLong 是 Java 中 AtomicLong 類的分布式“替代品”,用於在並發環境中保存長值。以下示例代碼演示了 RAtomicLong 的用法:

import org.redisson.Redisson; import org.redisson.api.RAtomicLong; import org.redisson.api.RedissonClient; public class AtomicLongExamples { public static void main(String[] args) { // 默認連接上127.0.0.1:6379 RedissonClient client = Redisson.create(); RAtomicLong atomicLong = client.getAtomicLong("myLong"); System.out.println("Init value: " + atomicLong.get()); atomicLong.incrementAndGet(); System.out.println("Current value: " + atomicLong.get()); atomicLong.addAndGet(10L); System.out.println("Final value: " + atomicLong.get()); client.shutdown(); } }