Golang crypto/tls.NewLRUClientSessionCache function example
package crypto/tls
NewLRUClientSessionCache returns a ClientSessionCache with the given capacity that uses an LRU strategy. If capacity is < 1, a default capacity is used instead.
Golang crypto/tls.NewLRUClientSessionCache function usage example
package main
import (
"crypto/tls"
"fmt"
)
func main() {
// LRU stands for Least Recently Used strategy
// http://en.wikipedia.org/wiki/Cache_algorithms
sessioncache := tls.NewLRUClientSessionCache(4)
clientsessionstate := make([]tls.ClientSessionState, 6)
sessionkey := []string{"ses1","ses2","ses3","ses4", "ses5","ses6"}
fmt.Println("Before LRU strategy")
//populate our sessioncache
for i := 0; i < 4; i++ {
sessioncache.Put(sessionkey[i], &clientsessionstate[i])
}
//list our sessioncache
for i := 0; i < 4; i++ {
fmt.Println(sessioncache.Get(sessionkey[i]))
}
fmt.Println("AFTER LRU ")
// Add two entries and the first 2 should be removed
for i := 4; i < 6; i++ {
sessioncache.Put(sessionkey[i], &clientsessionstate[i])
}
//Removed sessionkey will return false
for i := 0; i < 4; i++ {
fmt.Println(sessioncache.Get(sessionkey[i]))
}
}
Output :
Before LRU strategy
&{[] 0 0 [] []} true
&{[] 0 0 [] []} true
&{[] 0 0 [] []} true
&{[] 0 0 [] []} true
AFTER LRU
false
false &{[] 0 0 [] []} true
&{[] 0 0 [] []} true
Reference :
Advertisement
Something interesting
Tutorials
+4.6k JavaScript : Rounding number to decimal formats to display currency
+9.1k Golang : Serving HTTP and Websocket from different ports in a program example
+6k Linux/MacOSX : Search for files by filename and extension with find command
+10.6k Golang : Get local time and equivalent time in different time zone
+7.4k Golang : Hue, Saturation and Value(HSV) with OpenCV example
+11.6k Get form post value in Go
+30.4k Golang : Generate random string
+5.8k Golang : Launching your executable inside a console under Linux
+14.6k Golang : Send email with attachment(RFC2822) using Gmail API example
+6.2k PHP : Get client IP address
+25.3k Golang : Convert uint value to string type