Skip to content

Commit

Permalink
Refactor and document the utils package.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielcoderX committed Sep 4, 2023
1 parent 796277f commit 8153880
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
25 changes: 23 additions & 2 deletions utils/cache.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package utils provides utility functions including a cache implementation.
package utils

import (
Expand All @@ -21,10 +22,30 @@ func (item Item) Expired() bool {
return time.Now().UnixNano() > item.Expiration
}

// Cache is the cache object
// Cache is a thread-safe in-memory cache implementation with expiration support.
// It allows you to store key-value pairs with optional expiration times.
//
// Usage:
//
// // Create a new cache with a 10-minute default expiration time.
// myCache := NewCache(10 * time.Minute)
//
// // Store a value with a key and an optional expiration duration.
// myCache.Set("myKey", myValue)
//
// // Retrieve a value from the cache. Returns the value and true if found, or nil and false if not found.
// value, found := myCache.Get("myKey")
//
// // Check if a key exists in the cache.
// if found {
// // Value exists in the cache.
// fmt.Println(value)
// } else {
// // Value not found in the cache.
// fmt.Println("Key not found")
// }
type Cache struct {
*cache
// If this is confusing, see the comment at the bottom of New()
}

type cache struct {
Expand Down
2 changes: 2 additions & 0 deletions utils/helper.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package utils provides utility functions for the application.
package utils

import (
Expand All @@ -7,6 +8,7 @@ import (
"strings"
)

// WSEndpointHelper generates a WebSocket endpoint URL based on the workerAddress, rawDestAddress, and network.
func WSEndpointHelper(workerAddress, rawDestAddress, network string) (string, error) {
u, err := url.Parse(workerAddress)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package utils provides utility functions for the application.
package utils

import (
Expand All @@ -6,6 +7,7 @@ import (

var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-"

// ShortID generates a random short ID of the specified length.
func ShortID(length int) string {
ll := len(chars)
b := make([]byte, length)
Expand Down

0 comments on commit 8153880

Please sign in to comment.