API Reference
Global Functions
CachedTimeNano
func CachedTimeNano() int64
Returns the cached time in nanoseconds since Unix epoch from the default cache. This function provides zero-allocation access to the current timestamp and is the fastest way to get time information.
Returns: int64 - Nanoseconds since January 1, 1970 UTC
Performance: Zero allocation, atomic read operation
Example:
nano := timecache.CachedTimeNano()
fmt.Printf("Timestamp: %d nanoseconds\n", nano)
CachedTime
func CachedTime() time.Time
Returns the cached time as a time.Time value from the default cache. This function converts the internal nanosecond timestamp to a time.Time for convenient use with Go's time package functions.
Returns: time.Time - Current cached time
Performance: Minimal allocation for time.Time conversion
Example:
now := timecache.CachedTime()
fmt.Printf("Current time: %v\n", now)
CachedTimeString
func CachedTimeString() string
Returns the cached time formatted as an RFC3339Nano string from the default cache. The returned string is in UTC timezone and follows the format: "2006-01-02T15:04:05.999999999Z07:00"
Returns: string - Formatted time string in RFC3339Nano format
Performance: String allocation for formatting
Example:
timeStr := timecache.CachedTimeString()
fmt.Printf("ISO timestamp: %s\n", timeStr)
DefaultCache
func DefaultCache() *TimeCache
Returns the global default TimeCache instance. This allows access to the default cache for advanced operations like checking resolution or stopping the cache.
Returns: *TimeCache - The default cache instance
Example:
defaultCache := timecache.DefaultCache()
fmt.Printf("Default cache resolution: %v\n", defaultCache.Resolution())
StopDefaultCache
func StopDefaultCache()
Stops the global default time cache. After calling this function, the default cache will no longer be updated and the background goroutine will terminate.
Note: This function is mainly intended for testing and shutdown scenarios. In normal application usage, the default cache should remain running.
Example:
// During application shutdown
timecache.StopDefaultCache()
Cache Creation Functions
func New() *TimeCache
Creates a new TimeCache with default resolution (500µs). The default resolution provides a good balance between accuracy and CPU usage for most high-throughput applications.
Returns: *TimeCache - New cache instance.
Performance: Starts background goroutine immediately.
Example:
tc := timecache.New()
defer tc.Stop()
now := tc.CachedTime()
NewWithResolution
func NewWithResolution(resolution time.Duration) *TimeCache
Creates a new TimeCache with custom update resolution. The resolution parameter controls how frequently the cached time is updated. Smaller values provide more accurate timestamps but consume more CPU.
Parameters:
resolution time.Duration- The update interval for the cache
Returns: *TimeCache - New cache instance with custom resolution
Recommended resolution values:
- 100µs to 500µs: High precision, suitable for real-time systems
- 1ms to 10ms: Balanced performance, good for most applications
- >10ms: Minimal CPU impact, suitable for non-critical timing
Example:
// High precision cache for real-time logging
tc := timecache.NewWithResolution(100 * time.Microsecond)
defer tc.Stop()
// Balanced cache for general use
tc2 := timecache.NewWithResolution(1 * time.Millisecond)
defer tc2.Stop()
TimeCache Methods
CachedTimeNano
func (tc *TimeCache) CachedTimeNano() int64
Returns the cached time in nanoseconds since Unix epoch. This method provides zero-allocation access to the current timestamp and is the fastest way to get time information from the cache.
Performance: Zero allocation, atomic read operation
Returns: int64: Nanoseconds since January 1, 1970 UTC
Example:
tc := timecache.New()
defer tc.Stop()
nano := tc.CachedTimeNano()
fmt.Printf("Timestamp: %d nanoseconds\n", nano)
CachedTime
func (tc *TimeCache) CachedTime() time.Time
Returns the cached time as a time.Time value. This method converts the internal nanosecond timestamp to a time.Time for convenient use with Go's time package functions.
Returns: time.Time: The cached time
Performance: Minimal allocation for time.Time conversion
Example:
tc := timecache.New()
defer tc.Stop()
now := tc.CachedTime()
fmt.Printf("Current time: %v\n", now)
CachedTimeString
func (tc *TimeCache) CachedTimeString() string
Returns the cached time formatted as an RFC3339Nano string. The returned string is in UTC timezone and follows the format: "2006-01-02T15:04:05.999999999Z07:00"
Returns: string: Formatted time string in RFC3339Nano format
Performance: String allocation for formatting
Example:
tc := timecache.New()
defer tc.Stop()
timeStr := tc.CachedTimeString()
fmt.Printf("ISO timestamp: %s\n", timeStr)
Resolution
func (tc *TimeCache) Resolution() time.Duration
Returns the update frequency of this cache. This is the interval at which the cached time value is refreshed by the background updater goroutine.
Returns: time.Duration - The update frequency of the cache
Example:
tc := timecache.NewWithResolution(1 * time.Millisecond)
defer tc.Stop()
fmt.Printf("Cache updates every: %v\n", tc.Resolution())
Stop
func (tc *TimeCache) Stop()
Permanently stops the time cache updater. After calling Stop, the cached time value will no longer be updated and the background goroutine will terminate.
Important: It is important to call Stop to prevent goroutine leaks when the cache is no longer needed.
Example:
tc := timecache.New()
// ... use the cache ...
tc.Stop() // Clean up resources
Performance Characteristics
| Method | Allocations | Performance |
|---|---|---|
CachedTimeNano() |
0 | Fastest |
CachedTime() |
1 (time.Time) | Fast |
CachedTimeString() |
1 (string) | Moderate |
Error Handling
go-timecache is designed to be robust and doesn't return errors in normal operation. The only potential issues are:
- Goroutine leaks: Always call
Stop()on custom caches. - Stale time: After calling
Stop(), the cached time becomes stale.