github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/cache/measurable.go (about)

     1  // Copyright 2017 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package cache
     6  
     7  // Measurable is an interface for types whose size is measurable.
     8  type Measurable interface {
     9  	// Size returns the size of the object, in bytes, including both statically
    10  	// and dynamically sized parts.
    11  	Size() int
    12  }
    13  
    14  // memoizedMeasurable is a wrapper around a Measurable that memoizes the size
    15  // to avoid frequent size calculations.
    16  //
    17  // Note that if the size of the Measurable
    18  // changes after memoizedMeasurable memoizes the size, it won't be updated
    19  // automatically.
    20  type memoizedMeasurable struct {
    21  	m    Measurable
    22  	size int
    23  }
    24  
    25  // Size implements the Measurable interface.
    26  func (m memoizedMeasurable) Size() int {
    27  	if m.size <= 0 {
    28  		m.size = m.m.Size()
    29  	}
    30  	return m.size
    31  }