Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A golang map in which entries expire after given a time period

License

NotificationsYou must be signed in to change notification settings

jftuga/TtlMap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TtlMap is golang package that implements atime-to-live map such that after a given amount of time, items in the map are deleted.

  • The map key can be anycomparable data type, via Generics.
  • Any data type can be used as a map value. Internally,interface{} is used for this.

Example

Full example using many data types

Small example:

package mainimport ("fmt""time""github.com/jftuga/TtlMap")funcmain() {maxTTL:=time.Duration(time.Second*4)// a key's time to live in secondsstartSize:=3// initial number of items in mappruneInterval:=time.Duration(time.Second*1)// search for expired items every 'pruneInterval' secondsrefreshLastAccessOnGet:=true// update item's 'lastAccessTime' on a .Get()// any comparable data type such as int, uint64, pointers and struct types (if all field types are comparable)// can be used as the key type, not just stringt:=TtlMap.New[string](maxTTL,startSize,pruneInterval,refreshLastAccessOnGet)defert.Close()// populate the TtlMapt.Put("myString","a b c")t.Put("int_array", []int{1,2,3})fmt.Println("TtlMap length:",t.Len())// display all items in TtlMapall:=t.All()fork,v:=rangeall {fmt.Printf("[%9s] %v\n",k,v.Value)}fmt.Println()sleepTime:=maxTTL+pruneIntervalfmt.Printf("Sleeping %v seconds, items should be 'nil' after this time\n",sleepTime)time.Sleep(sleepTime)fmt.Printf("[%9s] %v\n","myString",t.Get("myString"))fmt.Printf("[%9s] %v\n","int_array",t.Get("int_array"))fmt.Println("TtlMap length:",t.Len())}

Output:

$ go run small.goTtlMap length: 2[ myString] a b c[int_array] [1 2 3]Sleeping 5 seconds, items should be 'nil' after this time[ myString] <nil>[int_array] <nil>TtlMap length: 0

API functions

  • New: initialize aTtlMap
  • Close: this stops the goroutine that checks for expired items; use withdefer
  • Len: return the number of items in the map
  • Put: add a key/value
  • Get: get the current value of the given key; returnnil if the key is not found in the map
  • GetNoUpdate: same asGet, but do not update thelastAccess expiration time
      • This ignores therefreshLastAccessOnGet parameter
  • All: returns acopy of all items in the map
  • Delete: delete an item; returntrue if the item was deleted,false if the item was not found in the map
  • Clear: remove all items from the map

Performance

  • Searching for expired items runs in O(n) time, where n = number of items in theTtlMap.
    • This inefficiency can be somewhat mitigated by increasing the value of thepruneInterval time.
  • In most cases you wantpruneInterval > maxTTL; otherwise expired items will stay in the map longer than expected.

Acknowledgments

Disclosure Notification

This program was completely developed on my own personal time, for my own personal benefit, and on my personally owned equipment.

About

A golang map in which entries expire after given a time period

Topics

Resources

License

Stars

Watchers

Forks

Languages


[8]ページ先頭

©2009-2025 Movatter.jp