Skip to content

Commit 5b17094

Browse files
authored
Code Optimization (#95)
* Code Optimization * Rename Handler to SlogHandler in logger package Refactored Handler methods to SlogHandler for clarity and consistency in both Logger and default logger functions.
1 parent ab42a6f commit 5b17094

File tree

5 files changed

+125
-115
lines changed

5 files changed

+125
-115
lines changed

cache/renew.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@ package cache
33
import (
44
"context"
55
"log"
6-
"sync"
76
"time"
87

98
"github.com/sunshineplan/utils/container"
109
)
1110

1211
type item[T any] struct {
13-
sync.Mutex
1412
ctx context.Context
1513
cancel context.CancelFunc
1614
lifecycle time.Duration
17-
value T
15+
value container.Value[T]
1816
fn func() (T, error)
1917
}
2018

@@ -23,16 +21,14 @@ func (i *item[T]) set(value T) {
2321
if i.lifecycle > 0 {
2422
i.ctx, i.cancel = context.WithTimeout(i.ctx, i.lifecycle)
2523
}
26-
i.value = value
24+
i.value.Store(value)
2725
}
2826

2927
func (i *item[T]) renew() T {
3028
v, err := i.fn()
31-
i.Lock()
32-
defer i.Unlock()
3329
if err != nil {
3430
log.Print(err)
35-
v = i.value
31+
return i.value.Load()
3632
}
3733
i.set(v)
3834
return v
@@ -92,17 +88,13 @@ func (c *CacheWithRenew[Key, Value]) Get(key Key) (value Value, ok bool) {
9288
if i, ok = c.get(key); !ok {
9389
return
9490
}
95-
i.Lock()
96-
defer i.Unlock()
97-
value = i.value
91+
value = i.value.Load()
9892
return
9993
}
10094

10195
// Delete deletes the value for a key.
10296
func (c *CacheWithRenew[Key, Value]) Delete(key Key) {
10397
if i, ok := c.m.LoadAndDelete(key); ok {
104-
i.Lock()
105-
defer i.Unlock()
10698
if i.cancel != nil {
10799
i.cancel()
108100
}
@@ -113,9 +105,7 @@ func (c *CacheWithRenew[Key, Value]) Delete(key Key) {
113105
func (c *CacheWithRenew[Key, Value]) Swap(key Key, value Value) (previous Value, loaded bool) {
114106
var i *item[Value]
115107
if i, loaded = c.get(key); loaded {
116-
i.Lock()
117-
defer i.Unlock()
118-
previous = i.value
108+
previous = i.value.Load()
119109
i.set(value)
120110
}
121111
return
@@ -125,8 +115,6 @@ func (c *CacheWithRenew[Key, Value]) Swap(key Key, value Value) (previous Value,
125115
func (c *CacheWithRenew[Key, Value]) Clear() {
126116
c.m.Range(func(key Key, i *item[Value]) bool {
127117
c.m.Delete(key)
128-
i.Lock()
129-
defer i.Unlock()
130118
if i.cancel != nil {
131119
i.cancel()
132120
}

container/value.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,49 @@ func (v *Value[T]) Swap(new T) (old T) {
4343
func (v *Value[T]) CompareAndSwap(old, new T) (swapped bool) {
4444
return v.v.CompareAndSwap(old, new)
4545
}
46+
47+
// An Int provides atomic operations on an integer value of type T.
48+
// It is a generic wrapper around [atomic.Int64], allowing usage with
49+
// signed integer types such as int, int8, int16, int32, and int64.
50+
type Int[T ~int64 | ~int32 | ~int16 | ~int8 | ~int] struct {
51+
v atomic.Int64
52+
}
53+
54+
// Add atomically adds delta to the value and returns the new value.
55+
func (v *Int[T]) Add(delta T) (new T) {
56+
return T(v.v.Add(int64(delta)))
57+
}
58+
59+
// And atomically performs a bitwise AND operation with mask and returns
60+
// the previous value.
61+
func (v *Int[T]) And(mask T) (old T) {
62+
return T(v.v.And(int64(mask)))
63+
}
64+
65+
// CompareAndSwap executes the compare-and-swap operation for the [Int].
66+
// It compares the current value with old, and if they are equal,
67+
// sets it to new and returns true. Otherwise, it returns false.
68+
func (v *Int[T]) CompareAndSwap(old T, new T) (swapped bool) {
69+
return v.v.CompareAndSwap(int64(old), int64(new))
70+
}
71+
72+
// Load atomically loads and returns the current value.
73+
func (v *Int[T]) Load() T {
74+
return T(v.v.Load())
75+
}
76+
77+
// Or atomically performs a bitwise OR operation with mask and returns
78+
// the previous value.
79+
func (v *Int[T]) Or(mask T) (old T) {
80+
return T(v.v.Or(int64(mask)))
81+
}
82+
83+
// Store atomically stores val into the [Int].
84+
func (v *Int[T]) Store(val T) {
85+
v.v.Store(int64(val))
86+
}
87+
88+
// Swap atomically stores new into the [Int] and returns the previous value.
89+
func (v *Int[T]) Swap(new T) (old T) {
90+
return T(v.v.Swap(int64(new)))
91+
}

log/log.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ func ErrorContext(ctx context.Context, msg string, args ...any) {
169169
Default().ErrorContext(ctx, msg, args...)
170170
}
171171

172-
// Handler returns the slog handler of the default Logger.
173-
func Handler() slog.Handler {
174-
return Default().Handler()
172+
// SlogHandler returns the slog handler of the default Logger.
173+
func SlogHandler() slog.Handler {
174+
return Default().SlogHandler()
175175
}
176176

177177
// Info logs a message at Info level using the default Logger.

log/logger.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ func (l *Logger) ErrorContext(ctx context.Context, msg string, args ...any) {
157157
l.slog.Load().ErrorContext(ctx, msg, args...)
158158
}
159159

160-
// Handler returns the current slog handler.
161-
func (l *Logger) Handler() slog.Handler {
160+
// SlogHandler returns the current slog handler.
161+
func (l *Logger) SlogHandler() slog.Handler {
162162
return l.slog.Load().Handler()
163163
}
164164

0 commit comments

Comments
 (0)