Transaction
E144EBB9831BFF…4FF690BEAA15
Block 26,367 · index 0 · indexed
Summary
- Hash
- E144EBB9831BFF19A9AAB8DE09F9A3496CDC9F5718049CCE9EFA4FF690BEAA15
- Block
- 26,367
- Size
- 6112 bytes
- Gas used
- 10,952,434 / 13,142,980
- Fee
- 1000000ugnot
- Memo
- gnopublish
- Status
- success
Messages
Arguments · 9
- #1lru
- #2README.md
- #3# LRU Cache > ⚠️ **Experimental — generated with no human supervision.** This realm was > produced automatically by an MCP-driven agent to exercise the gno MCP server > and tooling, and to generate test content for gno compilers, linters and > formatters. **Not audited. Not for production.** Full context & folder README: > [r/moul/x/daily](https://github.com/moul/gno-contracts/blob/main/r/moul/x/daily/README.md) --- A fixed-capacity (8) least-recently-used cache realm — a port of the classic LRU. `Put` inserts or updates an entry and marks it most-recently-used, evicting the least-recently-used entry when capacity is exceeded. `Get` returns a value, bumps its recency, and records a hit or a miss. `Render` shows the entries in MRU→LRU order alongside the capacity and hit/miss stats. Realm path: `gno.land/r/REPLACE_ADDR/lru` ## Example calls ``` # insert / update entries (marks them most-recently-used) Put("a", "1") Put("b", "2") # read a value (counts a hit, bumps recency) Get("a") # -> "1", true # read a missing key (counts a miss) Get("zzz") # -> "", false # clear everything Reset() # view state (MRU→LRU order + stats) Render("") ``` <!-- BEGIN GNOCONTRACTS FOOTER (generated by `make readmes`; do not edit below) --> --- Part of **[moul/gno-contracts](https://github.com/moul/gno-contracts)** — moul's versioned gno.land contracts. See the repository for the full catalog, build/test tooling, and usage. > 🧪 **Highly experimental — potentially vibe-coded.** Not audited; may break, change, or be removed at any time. Do not use with anything of value. Full disclaimer: [DISCLAIMER](https://github.com/moul/gno-contracts/blob/main/DISCLAIMER.md). <!-- END GNOCONTRACTS FOOTER -->
- #4gnomod.toml
- #5module = "gno.land/r/moul/x/daily/lru/v0" gno = "0.9"
- #6lru.gno
- #7package lru import ( "strconv" "strings" "chain" ) // capacity is the fixed maximum number of entries the cache holds. const capacity = 8 // node is a doubly-linked-list node holding a key/value pair. // The list is ordered MRU (head) -> LRU (tail). type node struct { key string value string prev *node next *node } // cache is a fixed-capacity LRU cache backed by a doubly-linked list and // an index from key -> node for O(1) lookups. type cache struct { index map[string]*node head *node // most-recently-used tail *node // least-recently-used size int hits int miss int } var c = &cache{index: make(map[string]*node)} // detach removes n from the linked list (does not touch the index). func (ca *cache) detach(n *node) { if n.prev != nil { n.prev.next = n.next } else { ca.head = n.next } if n.next != nil { n.next.prev = n.prev } else { ca.tail = n.prev } n.prev = nil n.next = nil } // pushFront inserts n at the head (MRU position). func (ca *cache) pushFront(n *node) { n.prev = nil n.next = ca.head if ca.head != nil { ca.head.prev = n } ca.head = n if ca.tail == nil { ca.tail = n } } // touch moves an existing node to the MRU position. func (ca *cache) touch(n *node) { ca.detach(n) ca.pushFront(n) } // evict removes the LRU entry (tail) and returns its key. func (ca *cache) evict() string { n := ca.tail if n == nil { return "" } ca.detach(n) delete(ca.index, n.key) ca.size-- return n.key } // Put inserts or updates key with value and marks it most-recently-used. // Evicts the least-recently-used entry if capacity is exceeded. func Put(cur realm, key string, value string) { if n, ok := c.index[key]; ok { n.value = value c.touch(n) chain.Emit("Put", "key", key, "op", "update") return } n := &node{key: key, value: value} c.index[key] = n c.pushFront(n) c.size++ evicted := "" if c.size > capacity { evicted = c.evict() } if evicted != "" { chain.Emit("Put", "key", key, "op", "insert", "evicted", evicted) } else { chain.Emit("Put", "key", key, "op", "insert") } } // Get returns the value for key and marks it most-recently-used. // It records a hit or a miss. The returned bool reports whether the key // was present. func Get(cur realm, key string) (string, bool) { if n, ok := c.index[key]; ok { c.touch(n) c.hits++ chain.Emit("Get", "key", key, "result", "hit") return n.value, true } c.miss++ chain.Emit("Get", "key", key, "result", "miss") return "", false } // Reset clears the cache and stats. func Reset(cur realm) { c = &cache{index: make(map[string]*node)} chain.Emit("Reset") } // peek returns the value without affecting recency or stats (read helper). func peek(key string) (string, bool) { if n, ok := c.index[key]; ok { return n.value, true } return "", false } // Render displays the cache contents in MRU->LRU order with stats. func Render(path string) string { var b strings.Builder b.WriteString("# LRU Cache\n\n") b.WriteString("A fixed-capacity least-recently-used cache.\n\n") b.WriteString("## Stats\n\n") b.WriteString("- Capacity: " + strconv.Itoa(capacity) + "\n") b.WriteString("- Size: " + strconv.Itoa(c.size) + "\n") b.WriteString("- Hits: " + strconv.Itoa(c.hits) + "\n") b.WriteString("- Misses: " + strconv.Itoa(c.miss) + "\n") total := c.hits + c.miss if total > 0 { // integer-percent hit rate, deterministic rate := (c.hits * 100) / total b.WriteString("- Hit rate: " + strconv.Itoa(rate) + "%\n") } else { b.WriteString("- Hit rate: n/a\n") } b.WriteString("\n") b.WriteString("## Entries (MRU → LRU)\n\n") if c.head == nil { b.WriteString("_empty_\n") return b.String() } b.WriteString("| # | Key | Value |\n") b.WriteString("|---|-----|-------|\n") i := 1 for n := c.head; n != nil; n = n.next { b.WriteString("| " + strconv.Itoa(i) + " | " + n.key + " | " + n.value + " |\n") i++ } return b.String() }
- #8/gno.MemPackageType
- #9 MPUserProd
Result log
msg:0,success:true,log:,events:[]