forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from opsramp/release/v0.106.x
Merge Release/v0.106.x to main 23Aug2024
- Loading branch information
Showing
17 changed files
with
652 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package lru | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
cache "github.com/hashicorp/golang-lru/v2/expirable" | ||
) | ||
|
||
type Cache struct { | ||
lrucache *cache.LRU[string, string] | ||
} | ||
|
||
const ( | ||
DEFAULT_CACHE_SIZE = 256 | ||
DEFAULT_CACHE_EXPIRATION_INTERVAL = 10 * time.Minute | ||
) | ||
|
||
var ( | ||
once sync.Once | ||
instance *Cache | ||
) | ||
|
||
func New(size int, expirationInterval time.Duration) *Cache { | ||
return &Cache{lrucache: cache.NewLRU[string, string](size, nil, expirationInterval)} | ||
} | ||
|
||
func GetInstance(size int, expirationInterval time.Duration) *Cache { | ||
once.Do(func() { | ||
instance = New(size, expirationInterval) | ||
}) | ||
return instance | ||
} | ||
|
||
func (c *Cache) Get(key string) (string, bool) { | ||
return c.lrucache.Get(key) | ||
} | ||
|
||
func (c *Cache) Add(key string, value string) { | ||
c.lrucache.Add(key, value) | ||
} | ||
|
||
func (c *Cache) AddEvicted(key string, value string) (evicted bool) { | ||
return c.lrucache.Add(key, value) | ||
} | ||
|
||
func (c *Cache) PrintKeys() { | ||
fmt.Println(c.lrucache.Keys()) | ||
} | ||
func (c *Cache) PrintValues() { | ||
fmt.Println(c.lrucache.Values()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package lru | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestLRU(t *testing.T) { | ||
cache := New(2, 0) | ||
|
||
// Test cases | ||
cache.Add("key1", "value1") | ||
cache.Add("key2", "value2") | ||
|
||
value, ok := cache.Get("key1") | ||
if !ok || value != "value1" { | ||
t.Errorf("Expected value1, got %v", value) | ||
} | ||
|
||
cache.Add("key3", "value3") | ||
|
||
_, ok = cache.Get("key4") | ||
if ok { | ||
t.Errorf("Expected key1 to be evicted") | ||
} | ||
} |
Oops, something went wrong.