Use temporary storage in a contract
Temporary storage is useful for a contract to store data that can quickly become irrelevant or out-dated. For example, here's how a contract might be used to store a recent price of BTC against the US Dollar.
// This function updates the BTC price
pub fn update_btc_price(env: Env, price: i128) {
env.storage().temporary().set(&!symbol_short("BTC"), &price);
}
// This function reads and returns the current BTC price (zero if the storage
// entry is archived)
pub fn get_btc_price(env: Env) -> i128 {
if let Some(price) = env.storage().temporary().get(&!symbol_short("BTC")) {
price
} else {
0
}
}
Guides in this category:
📄️ How to choose the right storage type for your use case
This guide walks you through choosing the most suitable storage type for your use case and how to implement it
📄️ Use instance storage in a contract
Instance storage has an archival TTL that is tied to the contract instance itself
📄️ Use persistent storage in a contract
Persistent storage can be useful for ledger entrys that are not common across every user of the contract instance
📄️ Use temporary storage in a contract
Temporary storage is useful for a contract to store data that can quickly become irrelevant or out-dated