multiversx-cache-patterns

Implement gas-optimized write-back and read-only cache patterns for MultiversX smart contracts using Rust's Drop trait.

12|5|Updated Jan 28, 2026
One-click install
npx skills add https://github.com/multiversx/mx-ai-skills --skill multiversx-cache-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: multiversx-cache-patterns
Source: https://github.com/multiversx/mx-ai-skills/tree/main/skills/multiversx-cache-patterns
Command: npx skills add https://github.com/multiversx/mx-ai-skills --skill multiversx-cache-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill addresses the high gas costs associated with frequent storage reads and writes in MultiversX smart contracts, providing patterns to significantly reduce transaction fees.

Core Features & Use Cases

  • Write-Back Caching: Implements a Drop-based cache to load state into memory, perform in-memory mutations, and commit all changes atomically on scope exit.
  • Read-Only Caching: Offers a pattern for view functions or read-heavy operations where state changes are not required, avoiding unnecessary storage reads.
  • Computed Values: Enables caching of derived values within the cache struct itself, preventing repeated computations and storage reads.
  • Use Case: For DeFi protocols or any gas-sensitive contract that performs multiple storage operations per transaction, this skill allows intermediate reads and writes to be free (in-memory), only incurring storage costs on initial load and final commit.

Quick Start

Implement the write-back cache pattern in your MultiversX smart contract endpoint by creating a StorageCache instance at the beginning of the function.

Frequently Asked Questions about multiversx-cache-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I reduce gas costs for storage reads and writes in MultiversX smart contracts?

To reduce MultiversX gas costs, implement in-memory caching patterns that load state into memory once and commit changes atomically on scope exit using Rust's Drop trait. This avoids repeated on-chain storage operations per transaction.

What is the best way to cache storage values in MultiversX Rust contracts?

The best way to cache storage values is using a write-back pattern that initializes a StorageCache struct at the beginning of an endpoint, performs mutations in-memory, and automatically commits to storage when the struct is dropped.

Can I use read-only caching for MultiversX view functions?

Yes, read-only caching is designed for MultiversX view functions and read-heavy operations where state changes are not required. This pattern prevents unnecessary storage reads by keeping accessed values in memory.

How does the Drop trait optimize gas in MultiversX smart contracts?

The Drop trait optimizes gas by automatically triggering the cache commit logic when the StorageCache instance goes out of scope. This batches all in-memory mutations into a single atomic storage write at the end of function execution.

When should I use write-back caching in MultiversX DeFi protocols?

Use write-back caching in MultiversX DeFi protocols when a single transaction requires multiple storage reads and writes. This approach makes intermediate operations free by handling them in-memory, charging gas only for initial load and final commit.

Does MultiversX caching support storing computed derived values?

Yes, MultiversX caching patterns support storing computed values directly within the cache struct. This prevents repeated computations and redundant storage reads by keeping derived data alongside the primary cached state.