Initial commit: web_hghl_server
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
package lock
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type IIdent interface {
|
||||
GetID() int64
|
||||
}
|
||||
|
||||
type Cache[T IIdent] struct {
|
||||
dict map[int64]T
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func NewCache[T IIdent]() *Cache[T] {
|
||||
return &Cache[T]{
|
||||
dict: make(map[int64]T),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache[T]) Get(Id int64) (T, bool) {
|
||||
c.lock.RLock()
|
||||
defer c.lock.RUnlock()
|
||||
t, ok := c.dict[Id]
|
||||
return t, ok
|
||||
}
|
||||
|
||||
func (c *Cache[T]) Keys() []int64 {
|
||||
c.lock.RLock()
|
||||
defer c.lock.RUnlock()
|
||||
keys := make([]int64, 0, len(c.dict))
|
||||
for k := range c.dict {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
func (c *Cache[T]) Set(items ...T) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
for _, item := range items {
|
||||
c.dict[item.GetID()] = item
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache[T]) Del(items ...T) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
for _, item := range items {
|
||||
delete(c.dict, item.GetID())
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache[T]) Update(dict map[int64]T) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
c.dict = dict
|
||||
}
|
||||
|
||||
func (c *Cache[T]) SetItems(items []T) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
for _, item := range items {
|
||||
c.dict[item.GetID()] = item
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache[T]) Has(Id int64) bool {
|
||||
c.lock.RLock()
|
||||
defer c.lock.RUnlock()
|
||||
_, ok := c.dict[Id]
|
||||
return ok
|
||||
}
|
||||
|
||||
func (c *Cache[T]) GetItems(Ids []int64) []T {
|
||||
ret := make([]T, 0)
|
||||
c.lock.RLock()
|
||||
defer c.lock.RUnlock()
|
||||
for _, id := range Ids {
|
||||
if item, ok := c.dict[id]; ok {
|
||||
ret = append(ret, item)
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package lock
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Dict[K comparable, V any] struct {
|
||||
dict map[K]V
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func NewDict[K comparable, V any]() *Dict[K, V] {
|
||||
return &Dict[K, V]{
|
||||
dict: make(map[K]V),
|
||||
}
|
||||
}
|
||||
|
||||
// Count get dict key count
|
||||
func (d *Dict[K, V]) Count() int {
|
||||
d.lock.RLock()
|
||||
defer d.lock.RUnlock()
|
||||
return len(d.dict)
|
||||
}
|
||||
|
||||
// Keys get dict keys
|
||||
func (d *Dict[K, V]) Keys() []K {
|
||||
d.lock.RLock()
|
||||
defer d.lock.RUnlock()
|
||||
keys := make([]K, 0, len(d.dict))
|
||||
for k := range d.dict {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
// Set set k v to dict, overwrite old value if exists
|
||||
func (d *Dict[K, V]) Set(key K, value V) {
|
||||
d.lock.Lock()
|
||||
defer d.lock.Unlock()
|
||||
d.dict[key] = value
|
||||
}
|
||||
|
||||
// Has if exists, return true
|
||||
func (d *Dict[K, V]) Has(key K) bool {
|
||||
d.lock.RLock()
|
||||
defer d.lock.RUnlock()
|
||||
_, exists := d.dict[key]
|
||||
return exists
|
||||
}
|
||||
|
||||
// Get if exists, return value and true
|
||||
func (d *Dict[K, V]) Get(key K) (V, bool) {
|
||||
d.lock.RLock()
|
||||
defer d.lock.RUnlock()
|
||||
value, exists := d.dict[key]
|
||||
return value, exists
|
||||
}
|
||||
|
||||
// Del if exists and del success, return true
|
||||
func (d *Dict[K, V]) Del(key K) bool {
|
||||
d.lock.Lock()
|
||||
defer d.lock.Unlock()
|
||||
_, exists := d.dict[key]
|
||||
delete(d.dict, key)
|
||||
return exists
|
||||
}
|
||||
|
||||
// Remove if exists and remove success, return old value and true
|
||||
func (d *Dict[K, V]) Remove(key K) (V, bool) {
|
||||
d.lock.Lock()
|
||||
defer d.lock.Unlock()
|
||||
value, exists := d.dict[key]
|
||||
delete(d.dict, key)
|
||||
return value, exists
|
||||
}
|
||||
|
||||
// SetIfNotExists if not exists and set success, return true
|
||||
func (d *Dict[K, V]) SetIfNotExists(key K, value V) bool {
|
||||
d.lock.Lock()
|
||||
defer d.lock.Unlock()
|
||||
_, exists := d.dict[key]
|
||||
if !exists {
|
||||
d.dict[key] = value
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// GetOrSetIfNotExists if key exists return old:true, or, set new value, and return new:false
|
||||
func (d *Dict[K, V]) GetOrSetIfNotExists(key K, new V) (V, bool) {
|
||||
d.lock.Lock()
|
||||
defer d.lock.Unlock()
|
||||
old, exists := d.dict[key]
|
||||
if !exists {
|
||||
d.dict[key] = new
|
||||
return new, false
|
||||
}
|
||||
return old, true
|
||||
}
|
||||
|
||||
// Travel travels all dict keys and values
|
||||
func (d *Dict[K, V]) Travel(travel func(key K, value V) bool) {
|
||||
d.lock.RLock()
|
||||
defer d.lock.RUnlock()
|
||||
for key, value := range d.dict {
|
||||
cont := travel(key, value)
|
||||
if !cont {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clear dict directly
|
||||
func (d *Dict[K, V]) Clear() {
|
||||
d.lock.Lock()
|
||||
defer d.lock.Unlock()
|
||||
d.dict = map[K]V{}
|
||||
}
|
||||
|
||||
// Update replace dict directly
|
||||
func (d *Dict[K, V]) Update(dict map[K]V) {
|
||||
d.lock.Lock()
|
||||
defer d.lock.Unlock()
|
||||
d.dict = dict
|
||||
}
|
||||
|
||||
// Replace replace dict directly and return old dict
|
||||
func (d *Dict[K, V]) Replace(dict map[K]V) map[K]V {
|
||||
d.lock.Lock()
|
||||
defer d.lock.Unlock()
|
||||
old := d.dict
|
||||
d.dict = dict
|
||||
return old
|
||||
}
|
||||
|
||||
// Append dict directly
|
||||
func (d *Dict[K, V]) Append(dict map[K]V) {
|
||||
d.lock.Lock()
|
||||
defer d.lock.Unlock()
|
||||
for k, v := range dict {
|
||||
d.dict[k] = v
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package lock
|
||||
|
||||
import "sync"
|
||||
|
||||
// Queue 定义一个泛型队列结构
|
||||
type Queue[T any] struct {
|
||||
items []T
|
||||
lock sync.RWMutex // 添加读写锁
|
||||
}
|
||||
|
||||
// NewQueue 创建一个新的队列
|
||||
func NewQueue[T any]() *Queue[T] {
|
||||
return &Queue[T]{
|
||||
items: make([]T, 0),
|
||||
}
|
||||
}
|
||||
|
||||
// Enqueue 入队操作 - 将元素添加到队列末尾
|
||||
func (q *Queue[T]) Enqueue(item T) {
|
||||
q.lock.Lock() // 写操作需要写锁
|
||||
defer q.lock.Unlock()
|
||||
|
||||
q.items = append(q.items, item)
|
||||
}
|
||||
|
||||
// Dequeue 出队操作 - 从队列头部移除并返回元素
|
||||
func (q *Queue[T]) Dequeue() (T, bool) {
|
||||
q.lock.Lock() // 写操作需要写锁
|
||||
defer q.lock.Unlock()
|
||||
|
||||
var zero T
|
||||
if len(q.items) == 0 {
|
||||
return zero, false
|
||||
}
|
||||
|
||||
item := q.items[0]
|
||||
q.items = q.items[1:]
|
||||
return item, true
|
||||
}
|
||||
|
||||
// Peek 查看队列头部元素但不移除
|
||||
func (q *Queue[T]) Peek() (T, bool) {
|
||||
q.lock.RLock() // 读操作使用读锁
|
||||
defer q.lock.RUnlock()
|
||||
|
||||
var zero T
|
||||
if len(q.items) == 0 {
|
||||
return zero, false
|
||||
}
|
||||
return q.items[0], true
|
||||
}
|
||||
|
||||
// IsEmpty 检查队列是否为空
|
||||
func (q *Queue[T]) IsEmpty() bool {
|
||||
q.lock.RLock() // 读操作使用读锁
|
||||
defer q.lock.RUnlock()
|
||||
|
||||
return len(q.items) == 0
|
||||
}
|
||||
|
||||
// Size 返回队列中的元素个数
|
||||
func (q *Queue[T]) Size() int {
|
||||
q.lock.RLock() // 读操作使用读锁
|
||||
defer q.lock.RUnlock()
|
||||
|
||||
return len(q.items)
|
||||
}
|
||||
|
||||
// Clear 清空队列
|
||||
func (q *Queue[T]) Clear() {
|
||||
q.lock.Lock() // 写操作需要写锁
|
||||
defer q.lock.Unlock()
|
||||
|
||||
q.items = make([]T, 0)
|
||||
}
|
||||
Reference in New Issue
Block a user