github.com/blend/go-sdk@v1.20240719.1/cache/lru_heap_values.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package cache
     9  
    10  import (
    11  	"container/heap"
    12  )
    13  
    14  var (
    15  	_ heap.Interface = (*LRUHeapValues)(nil)
    16  )
    17  
    18  // LRUHeapValues is an alias that allows for use of a Value array as a heap
    19  // storage backend. It also implements sorting for other use cases.
    20  type LRUHeapValues []*Value
    21  
    22  // Len returns the values length.
    23  func (lruv LRUHeapValues) Len() int { return len(lruv) }
    24  
    25  // Less returns if two values are strictly less than eachother.
    26  func (lruv LRUHeapValues) Less(i, j int) bool { return lruv[i].Expires.Before(lruv[j].Expires) }
    27  
    28  // Swap swaps values at the given positions.
    29  func (lruv LRUHeapValues) Swap(i, j int) { lruv[i], lruv[j] = lruv[j], lruv[i] }
    30  
    31  // Push adds a new item.
    32  func (lruv *LRUHeapValues) Push(x interface{}) {
    33  	*lruv = append(*lruv, x.(*Value))
    34  }
    35  
    36  // Pop returns an item.
    37  func (lruv *LRUHeapValues) Pop() interface{} {
    38  	old := *lruv
    39  	n := len(old)
    40  	x := old[n-1]
    41  	*lruv = old[0 : n-1]
    42  	return x
    43  }