最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Rust在寫庫時實現(xiàn)緩存的操作方法

 更新時間:2024年01月06日 15:37:51   作者:Star-tears  
Moka是一個用于Rust的高性能緩存庫,它提供了多種類型的緩存數(shù)據(jù)結(jié)構(gòu),包括哈希表、LRU(最近最少使用)緩存和?支持TTL(生存時間)緩存,這篇文章給大家介紹Rust在寫庫時實現(xiàn)緩存的相關(guān)知識,感興趣的朋友一起看看吧

Rust在寫庫時實現(xiàn)緩存

依賴

在寫庫時,實現(xiàn)一個緩存請求,需要用到全局變量,所以我們可以添加cratelazy_static

Cargo.toml添加以下依賴

[dependencies]
chrono = "0.4.31"
lazy_static = "1.4.0"
reqwest = { version = "0.11.23", features = ["blocking", "json"] }
serde = { version = "1.0.193", features = ["derive"] }
serde_json = "1.0.108"

代碼實現(xiàn)

use std::{sync::Mutex, collections::HashMap};
use chrono::{DateTime, Utc};
use lazy_static::lazy_static;
use serde_json::Value;
lazy_static! {
    static ref REQUESTS_RESPONSE_CACHE: Mutex<HashMap<String, RequestsResponseCache>> =
        Mutex::new(HashMap::new());
}
pub struct RequestsResponseCache {
    pub response: Value,
    pub datetime: DateTime<Utc>,
}
pub fn get_requests_response_cache(url: &str) -> Result<Value, reqwest::Error> {
    let mut cache = REQUESTS_RESPONSE_CACHE.lock().unwrap();
    if let Some(cache_entry) = cache.get(url) {
        let elapsed = Utc::now() - cache_entry.datetime;
        if elapsed.num_seconds() > 3600 {
            let response: Value = reqwest::blocking::get(url)?.json()?;
            let res = response.clone();
            let cache_entry = RequestsResponseCache {
                response,
                datetime: Utc::now(),
            };
            cache.insert(url.to_string(), cache_entry);
            return Ok(res);
        }
    }
    let response: Value = reqwest::blocking::get(url)?.json()?;
    let res = response.clone();
    let cache_entry = RequestsResponseCache {
        response,
        datetime: Utc::now(),
    };
    cache.insert(url.to_string(), cache_entry);
    Ok(res)
}

使用了 lazy_static 宏創(chuàng)建了一個靜態(tài)的全局變量 REQUESTS_RESPONSE_CACHE,這個全局變量是一個 Mutex 包裹的 HashMap,用來存儲請求的響應緩存。這個緩存是線程安全的,因為被 Mutex 包裹了起來,這樣就可以在多個線程中安全地訪問和修改這個緩存。

接著定義了一個 RequestsResponseCache 結(jié)構(gòu)體,用來表示緩存中的一個條目,其中包含了響應數(shù)據(jù) response 和緩存的時間戳 datetime。

然后定義了一個 get_requests_response_cache 函數(shù),用來從緩存中獲取請求的響應。它首先嘗試從緩存中獲取指定 url 的響應數(shù)據(jù),如果緩存中有對應的條目,并且距離上次緩存的時間超過了 3600 秒(1 小時),則重新發(fā)起請求并更新緩存,然后返回響應數(shù)據(jù)。如果緩存中沒有對應的條目,或者緩存的時間未超過 3600 秒,則直接發(fā)起請求并更新緩存,然后返回響應數(shù)據(jù)。

這樣就提供了一個簡單的請求響應的緩存功能,能夠在需要時緩存請求的響應數(shù)據(jù),并在一定時間內(nèi)有效,從而減少對遠程服務的重復請求,提高程序性能。

補充:

rust緩存庫moka簡介

關(guān)于moka

“Moka” 是一個用于 Rust 的高性能緩存庫,它提供了多種類型的緩存數(shù)據(jù)結(jié)構(gòu),包括哈希表、LRU(最近最少使用)緩存和 支持TTL(生存時間)緩存。
以下是一些 “moka” 庫的特點和功能:

  • 多種緩存類型: “moka” 提供了多種緩存類型,包括哈希表緩存、LRU 緩存和 TTL 緩存。你可以根據(jù)具體的需求選擇適合的緩存類型。
  • 線程安全: “moka” 庫是線程安全的,可以在多線程環(huán)境中使用,不需要額外的同步措施。
  • 高性能: “moka” 的設計目標之一是提供高性能的緩存實現(xiàn)。它經(jīng)過優(yōu)化,能夠在高并發(fā)場景下快速處理緩存操作。
  • 可配置性: “moka” 允許你根據(jù)需要對緩存進行配置,如容量限制、緩存項的最大生存時間等。

moka的github地址:moka

moka的使用示例

1.事件通知:
支持在緩存項發(fā)生過期淘汰、用戶主動淘汰、緩存池大小受限強制淘汰時,觸發(fā)回調(diào)函數(shù)執(zhí)行一些后續(xù)任務。

use moka::{notification::RemovalCause, sync::Cache};
use std::time::{Duration,Instant};
fn main() {
    // 創(chuàng)建一個緩存項事件監(jiān)聽閉包
    let now = Instant::now();
    let listener = move |k, v: String, cause| {
        // 監(jiān)聽緩存項的觸發(fā)事件,RemovalCause包含四種場景:Expired(緩存項過期)、Explicit(用戶主動移除緩存)、Replaced(緩存項發(fā)生更新或替換)、Size(緩存數(shù)量達到上限驅(qū)逐)。
        println!(
            "== An entry has been evicted. time:{} k: {:?}, v: {:?},cause:{:?}",
            now.elapsed().as_secs(),
            k,
            v,
            cause
        );
        // 針對不同事項,進行處理。
        // match cause {
        //     RemovalCause::Expired => {}
        //     RemovalCause::Explicit => {}
        //     RemovalCause::Replaced => {}
        //     RemovalCause::Size => {}
        // }
    };
    //緩存生存時間:10s
    let ttl_time = Duration::from_secs(10);
    // 創(chuàng)建一個具有過期時間和淘汰機制的緩存
    let cache: Cache<String, String> = Cache::builder()
        .time_to_idle(ttl_time)
        .eviction_listener(listener)
        .build();
    // insert 緩存項
    cache.insert("key1".to_string(), "value1".to_string());
    cache.insert("key2".to_string(), "value2".to_string());
    cache.insert("key3".to_string(), "value3".to_string());
    // 5s后使用key1
    std::thread::sleep(Duration::from_secs(5));
    if let Some(value) = cache.get(&"key1".to_string()) {
        println!("5s: Value of key1: {}", value);
    }
    cache.remove("key3");
    println!("5s: remove key3");
    // 等待 6 秒,讓緩存項key2過期
    std::thread::sleep(Duration::from_secs(6));
    // 嘗試獲取緩存項 "key1" 的值
    if let Some(value) = cache.get("key1") {
        println!("11s: Value of key1: {}", value);
    } else {
        println!("Key1 has expired.");
    }
    // 嘗試獲取緩存項 "key2" 的值
    if let Some(value) = cache.get("key2") {
        println!("11s: Value of key2: {}", value);
    } else {
        println!("Key2 has expired.");
    }
    // 嘗試獲取緩存項 "key3" 的值
    if let Some(value) = cache.get("key3") {
        println!("11s: Value of key3: {}", value);
    } else {
        println!("Key3 has removed.");
    }
    // 空置9s后
    std::thread::sleep(Duration::from_secs(11));
    // 再次嘗試獲取緩存項 "key1" 的值
    if let Some(value) = cache.get("key1") {
        println!("22s: Value of key1: {}", value);
    } else {
        println!("Key1 has expired.");
    }
}

運行結(jié)果:

5s: Value of key1: value1
== An entry has been evicted. time:5 k: "key3", v: "value3",cause:Explicit
5s: remove key3
== An entry has been evicted. time:10 k: "key2", v: "value2",cause:Expired
11s: Value of key1: value1
Key2 has expired.
Key3 has removed.
== An entry has been evicted. time:21 k: "key1", v: "value1",cause:Expired
Key1 has expired.

2.支持同步并發(fā):

use moka::sync::Cache;
use std::thread;
fn value(n: usize) -> String {
    format!("value {}", n)
}
fn main() {
    const NUM_THREADS: usize = 3;
    const NUM_KEYS_PER_THREAD: usize = 2;
    // Create a cache that can store up to 6 entries.
    let cache = Cache::new(6);
    // Spawn threads and read and update the cache simultaneously.
    let threads: Vec<_> = (0..NUM_THREADS)
        .map(|i| {
            // To share the same cache across the threads, clone it.
            // This is a cheap operation.
            let my_cache = cache.clone();
            let start = i * NUM_KEYS_PER_THREAD;
            let end = (i + 1) * NUM_KEYS_PER_THREAD;
            thread::spawn(move || {
                // Insert 2 entries. (NUM_KEYS_PER_THREAD = 2)
                for key in start..end {
                    my_cache.insert(key, value(key));
                    println!("{}",my_cache.get(&key).unwrap());
                }
                // Invalidate every 2 element of the inserted entries.
                for key in (start..end).step_by(2) {
                    my_cache.invalidate(&key);
                }
            })
        })
        .collect();
    // Wait for all threads to complete.
    threads.into_iter().for_each(|t| t.join().expect("Failed"));
    // Verify the result.
    for key in 0..(NUM_THREADS * NUM_KEYS_PER_THREAD) {
        if key % 2 == 0 {
            assert_eq!(cache.get(&key), None);
        } else {
            assert_eq!(cache.get(&key), Some(value(key)));
        }
    }
}

結(jié)果:

value 2
value 3
value 0
value 4
value 1
value 5

并發(fā)讀寫cahce中的數(shù)據(jù)不會產(chǎn)生異常。

3.下面是moka庫example中給出的異步示例:

use moka::future::Cache;
#[tokio::main]
async fn main() {
    const NUM_TASKS: usize = 16;
    const NUM_KEYS_PER_TASK: usize = 64;
    fn value(n: usize) -> String {
        format!("value {}", n)
    }
    // Create a cache that can store up to 10,000 entries.
    let cache = Cache::new(10_000);
    // Spawn async tasks and write to and read from the cache.
    let tasks: Vec<_> = (0..NUM_TASKS)
        .map(|i| {
            // To share the same cache across the async tasks, clone it.
            // This is a cheap operation.
            let my_cache = cache.clone();
            let start = i * NUM_KEYS_PER_TASK;
            let end = (i + 1) * NUM_KEYS_PER_TASK;
            tokio::spawn(async move {
                // Insert 64 entries. (NUM_KEYS_PER_TASK = 64)
                for key in start..end {
                    // insert() is an async method, so await it.
                    my_cache.insert(key, value(key)).await;
                    // get() returns Option<String>, a clone of the stored value.
                    assert_eq!(my_cache.get(&key), Some(value(key)));
                }
                // Invalidate every 4 element of the inserted entries.
                for key in (start..end).step_by(4) {
                    // invalidate() is an async method, so await it.
                    my_cache.invalidate(&key).await;
                }
            })
        })
        .collect();
    // Wait for all tasks to complete.
    futures_util::future::join_all(tasks).await;
    // Verify the result.
    for key in 0..(NUM_TASKS * NUM_KEYS_PER_TASK) {
        if key % 4 == 0 {
            assert_eq!(cache.get(&key), None);
        } else {
            assert_eq!(cache.get(&key), Some(value(key)));
        }
    }
}

到此這篇關(guān)于Rust在寫庫時實現(xiàn)緩存的文章就介紹到這了,更多相關(guān)Rust緩存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • rust實現(xiàn)post小程序(完整代碼)

    rust實現(xiàn)post小程序(完整代碼)

    這篇文章主要介紹了rust實現(xiàn)一個post小程序,本文通過示例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-04-04
  • Rust 多線程編程的實現(xiàn)

    Rust 多線程編程的實現(xiàn)

    在rust中,多線程編程不算困難,但是也需要留心和別的編程語言中不同的地方,本文主要介紹了Rust 多線程編程的實現(xiàn),感興趣的可以了解一下
    2023-12-12
  • Rust中類型轉(zhuǎn)換在錯誤處理中的應用小結(jié)

    Rust中類型轉(zhuǎn)換在錯誤處理中的應用小結(jié)

    隨著項目的進展,關(guān)于Rust的故事又翻開了新的一頁,今天來到了服務器端的開發(fā)場景,發(fā)現(xiàn)錯誤處理中的錯誤類型轉(zhuǎn)換有必要分享一下,對Rust錯誤處理相關(guān)知識感興趣的朋友一起看看吧
    2023-09-09
  • RUST語言函數(shù)的定義與調(diào)用方法

    RUST語言函數(shù)的定義與調(diào)用方法

    定義一個RUST函數(shù)使用fn關(guān)鍵字,下面通過本文給大家介紹RUST語言函數(shù)的定義與調(diào)用方法,感興趣的朋友跟隨小編一起看看吧
    2024-04-04
  • Rust語言之結(jié)構(gòu)體和枚舉的用途與高級功能詳解

    Rust語言之結(jié)構(gòu)體和枚舉的用途與高級功能詳解

    Rust 是一門注重安全性和性能的現(xiàn)代編程語言,其中結(jié)構(gòu)體和枚舉是其強大的數(shù)據(jù)類型之一,了解結(jié)構(gòu)體和枚舉的概念及其高級功能,將使你能夠更加靈活和高效地處理數(shù)據(jù),本文將深入探討 Rust 中的結(jié)構(gòu)體和枚舉,并介紹它們的用途和高級功能
    2023-10-10
  • Rust中向量的學習筆記

    Rust中向量的學習筆記

    在Rust語言中,向量是一種動態(tài)數(shù)組類型,可以存儲相同類型的元素,并且可以在運行時改變大小,本文就來介紹一下Rust中向量,感興趣的可以了解一下
    2024-03-03
  • 為什么要使用 Rust 語言、Rust 語言有什么優(yōu)勢

    為什么要使用 Rust 語言、Rust 語言有什么優(yōu)勢

    雖然 Rust 是一種通用的多范式語言,但它的目標是 C 和 C++占主導地位的系統(tǒng)編程領(lǐng)域,很多朋友會問rust語言難學嗎?rust語言可以做什么,今天帶著這些疑問通過本文詳細介紹下,感興趣的朋友一起看看吧
    2022-10-10
  • Rust常用特型之Drop特型

    Rust常用特型之Drop特型

    本文主要介紹了Rust常用特型之Drop特型,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-03-03
  • Rust之模式與模式匹配的實現(xiàn)

    Rust之模式與模式匹配的實現(xiàn)

    Rust中的模式匹配功能強大且靈活,它極大地提高了代碼的表達力和可讀性,本文主要介紹了Rust之模式與模式匹配,具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • rust標準庫std::env環(huán)境相關(guān)的常量

    rust標準庫std::env環(huán)境相關(guān)的常量

    在本章節(jié)中, 我們探討了Rust處理命令行參數(shù)的常見的兩種方式和處理環(huán)境變量的兩種常見方式, 拋開Rust的語法, 實際上在命令行參數(shù)的處理方式上, 與其它語言大同小異, 可能影響我們習慣的也就只剩下語法,本文介紹rust標準庫std::env的相關(guān)知識,感興趣的朋友一起看看吧
    2024-03-03

最新評論

永兴县| 大宁县| 华池县| 万源市| 全南县| 永靖县| 葵青区| 根河市| 玉田县| 霞浦县| 临湘市| 泗水县| 新安县| 巢湖市| 孟津县| 涡阳县| 林口县| 五原县| 新闻| 博客| 正安县| 伽师县| 阳西县| 章丘市| 齐河县| 福海县| 花莲市| 新蔡县| 沙湾县| 泰安市| 阆中市| 太仆寺旗| 乌拉特中旗| 泰顺县| 彭阳县| 乌兰县| 南充市| 察雅县| 故城县| 寻乌县| 青川县|