pub fn fibonacci_prime_cache(n: u64) -> u64Expand description
Primes the cached function fibonacci.
Example of how to use the cached attribute
ⓘ
// Example disabled due to missing cached dependency
use cached::proc_macro::cached;
#[cached(size = 100)]
pub fn expensive_calculation(x: u64) -> u64 {
// Expensive computation here
x * x
}Example of using TTL cache for memoization
This example shows how to use the TTL cache for memoizing expensive operations.
use scirs2_core::cache::TTLSizedCache;
use std::time::Duration;
// Create a TTL cache
let mut cache = TTLSizedCache::<String, String>::new(100, 60);
// Cache a value
let key = "example_key";
let value = "example_value";
cache.insert(key.to_string(), value.to_string());
// Retrieve a value
let retrieved_value = cache.get(&key.to_string());Compute Fibonacci numbers with memoization
This function demonstrates the use of memoization for computing Fibonacci numbers efficiently.
§Example
ⓘ
use cached::proc_macro::cached;
#[cached(size = 100)]
pub fn fibonacci_prime_cache(n: u64) -> u64 {
match n {
0 => 0,
1 => 1,
n => fibonacci_prime_cache(n - 1) + fibonacci_prime_cache(n - 2),
}
}§Arguments
n- The Fibonacci number to compute
§Returns
- The nth Fibonacci number
§Caching
This is a cached function that uses the FIBONACCI cached static.