欧美1区2区3区激情无套,两个女人互添下身视频在线观看,久久av无码精品人妻系列,久久精品噜噜噜成人,末发育娇小性色xxxx

Redis 字符串(String)

命令 描述 語(yǔ)法
SET 設(shè)置指定鍵的值 `SET key value [EX seconds] [PX milliseconds] [NX
GET 獲取指定鍵的值 GET key
INCR 將鍵對(duì)應(yīng)的值自增 1,如果鍵不存在,先將其值設(shè)為 0 再自增 INCR key
DECR 將鍵對(duì)應(yīng)的值自減 1,如果鍵不存在,先將其值設(shè)為 0 再自減 DECR key
INCRBY 將鍵對(duì)應(yīng)的值增加指定的整數(shù) INCRBY key increment
DECRBY 將鍵對(duì)應(yīng)的值減少指定的整數(shù) DECRBY key decrement
INCRBYFLOAT 將鍵對(duì)應(yīng)的值增加指定的浮點(diǎn)數(shù) INCRBYFLOAT key increment
SETNX 只有在鍵不存在時(shí),設(shè)置鍵的值 SETNX key value
SETEX 設(shè)置鍵的值,并同時(shí)設(shè)置過(guò)期時(shí)間(秒) SETEX key seconds value
PSETEX 設(shè)置鍵的值,并同時(shí)設(shè)置過(guò)期時(shí)間(毫秒) PSETEX key milliseconds value
MSET 同時(shí)設(shè)置一個(gè)或多個(gè)鍵值對(duì) MSET key1 value1 [key2 value2 ...]
MGET 同時(shí)獲取一個(gè)或多個(gè)鍵的值 MGET key1 [key2 ...]
GETSET 設(shè)置鍵的新值,并返回鍵的舊值 GETSET key value
STRLEN 獲取鍵對(duì)應(yīng)值的字符串長(zhǎng)度 STRLEN key

場(chǎng)景示例

  1. 緩存數(shù)據(jù) 假設(shè)我們有一個(gè)查詢(xún)數(shù)據(jù)庫(kù)獲取用戶(hù)信息的操作,為了減少數(shù)據(jù)庫(kù)壓力,我們可以將查詢(xún)結(jié)果緩存到 Redis 中。
import redis.clients.jedis.Jedis;

public class StringCacheExample {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("localhost");
        String userId = "1";
        String userInfo = "User details fetched from database";

        // 設(shè)置緩存,有效期 3600 秒(1 小時(shí))
        jedis.setex("user:" + userId, 3600, userInfo);

        // 模擬從緩存獲取數(shù)據(jù)
        String cachedUserInfo = jedis.get("user:" + userId);
        if (cachedUserInfo != null) {
            System.out.println("從緩存中獲取到用戶(hù)信息: " + cachedUserInfo);
        } else {
            System.out.println("緩存中未找到用戶(hù)信息,需查詢(xún)數(shù)據(jù)庫(kù)");
        }

        jedis.close();
    }
}
  1. 計(jì)數(shù)器 比如統(tǒng)計(jì)網(wǎng)站的訪(fǎng)問(wèn)量,每次有用戶(hù)訪(fǎng)問(wèn)時(shí),對(duì)計(jì)數(shù)器進(jìn)行自增操作。
import redis.clients.jedis.Jedis;

public class CounterExample {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("localhost");
        String counterKey = "website:visits";

        // 每次訪(fǎng)問(wèn)自增 1
        long newCount = jedis.incr(counterKey);
        System.out.println("當(dāng)前網(wǎng)站訪(fǎng)問(wèn)量: " + newCount);

        jedis.close();
    }
}
  1. 分布式鎖 在分布式系統(tǒng)中,為了保證同一時(shí)間只有一個(gè)服務(wù)實(shí)例執(zhí)行某個(gè)操作,可以使用 Redis 的 SETNX 命令實(shí)現(xiàn)簡(jiǎn)單的分布式鎖。
import redis.clients.jedis.Jedis;

public class DistributedLockExample {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("localhost");
        String lockKey = "resource:lock";
        String lockValue = System.currentTimeMillis() + 10000 + ""; // 鎖的過(guò)期時(shí)間(10 秒后)

        // 嘗試獲取鎖
        if (jedis.setnx(lockKey, lockValue) == 1) {
            try {
                System.out.println("獲取到鎖,執(zhí)行關(guān)鍵操作...");
                // 模擬關(guān)鍵操作
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                // 釋放鎖
                if (jedis.get(lockKey).equals(lockValue)) {
                    jedis.del(lockKey);
                    System.out.println("釋放鎖");
                }
            }
        } else {
            System.out.println("未獲取到鎖,等待或重試...");
        }

        jedis.close();
    }
}
  1. 使用 MSET 和 MGET 批量操作 假設(shè)我們需要批量存儲(chǔ)和獲取多個(gè)用戶(hù)的簡(jiǎn)單信息(如用戶(hù)名和年齡)。
import redis.clients.jedis.Jedis;
import java.util.HashMap;
import java.util.Map;

public class MSetMGetExample {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("localhost");
        Map<String, String> userInfoMap = new HashMap<>();
        userInfoMap.put("user:1:name", "Alice");
        userInfoMap.put("user:1:age", "30");
        userInfoMap.put("user:2:name", "Bob");
        userInfoMap.put("user:2:age", "25");

        // 批量設(shè)置鍵值對(duì)
        jedis.mset(userInfoMap);

        // 批量獲取鍵值對(duì)
        String[] keys = {"user:1:name", "user:1:age", "user:2:name", "user:2:age"};
        Map<String, String> resultMap = new HashMap<>();
        for (int i = 0; i < keys.length; i += 2) {
            resultMap.put(keys[i], jedis.get(keys[i]));
            resultMap.put(keys[i + 1], jedis.get(keys[i + 1]));
        }

        for (Map.Entry<String, String> entry : resultMap.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }

        jedis.close();
    }
}
  1. 使用 GETSET 實(shí)現(xiàn)數(shù)據(jù)更新并返回舊值 假設(shè)我們有一個(gè)記錄系統(tǒng)狀態(tài)的變量,每次更新?tīng)顟B(tài)時(shí)需要返回舊狀態(tài)。
import redis.clients.jedis.Jedis;

public class GetSetExample {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("localhost");
        String statusKey = "system:status";

        // 初始化狀態(tài)
        jedis.set(statusKey, "running");

        // 更新?tīng)顟B(tài)并獲取舊狀態(tài)
        String oldStatus = jedis.getSet(statusKey, "maintenance");
        System.out.println("舊系統(tǒng)狀態(tài): " + oldStatus);
        System.out.println("新系統(tǒng)狀態(tài): " + jedis.get(statusKey));

        jedis.close();
    }
}
  1. 使用 STRLEN 獲取字符串長(zhǎng)度 假設(shè)我們存儲(chǔ)了一篇文章內(nèi)容,需要獲取文章的字符長(zhǎng)度。
import redis.clients.jedis.Jedis;

public class StrLenExample {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("localhost");
        String articleKey = "article:1";
        String articleContent = "This is a sample article for testing STRLEN command.";

        jedis.set(articleKey, articleContent);
        long length = jedis.strlen(articleKey);
        System.out.println("文章字符長(zhǎng)度: " + length);

        jedis.close();
    }
}
#我的2024??透吖鈺r(shí)刻##??蛣?chuàng)作賞金賽##聊聊我眼中的AI#
全部評(píng)論

相關(guān)推薦

評(píng)論
點(diǎn)贊
收藏
分享

創(chuàng)作者周榜

更多
牛客網(wǎng)
??推髽I(yè)服務(wù)