feat(cache): 优化缓存系统并重构数据库连接管理

- 将Redis连接方式改为连接池模式,提升连接复用效率
- 修复缓存注释错误,统一标识数据库缓存逻辑
- 添加数据检索结果非空验证,避免空指针异常
- 在数据库操作中添加读写锁保护,确保并发安全性
- 实现数据库查询和执行操作的重试机制,增强稳定性
- 更新配置文件中的缓存和数据库设置,优化缓存策略
- 重构README文档,补充框架特性和性能测试数据
- 添加示例路由配置,完善快速入门指南
This commit is contained in:
2026-01-22 04:36:52 +08:00
parent 7843f7e8d6
commit 40f6783036
16 changed files with 2991 additions and 511 deletions
+39 -19
View File
@@ -3,7 +3,7 @@ package hotime
import (
. "code.hoteas.com/golang/hotime/cache"
. "code.hoteas.com/golang/hotime/common"
//"sync"
"sync"
)
// session对象
@@ -12,52 +12,72 @@ type SessionIns struct {
SessionId string
Map
ContextBase
//mutex sync.Mutex
mutex sync.RWMutex
}
func (that *SessionIns) set() {
that.HoTimeCache.Session(HEAD_SESSION_ADD+that.SessionId, that.Map)
// set 保存 session 到缓存,必须在锁内调用或传入深拷贝的 map
func (that *SessionIns) setWithCopy() {
// 深拷贝 Map 防止并发修改
that.mutex.RLock()
copyMap := make(Map, len(that.Map))
for k, v := range that.Map {
copyMap[k] = v
}
that.mutex.RUnlock()
that.HoTimeCache.Session(HEAD_SESSION_ADD+that.SessionId, copyMap)
}
func (that *SessionIns) Session(key string, data ...interface{}) *Obj {
that.mutex.Lock()
if that.Map == nil {
that.get()
that.getWithoutLock()
}
that.mutex.Unlock()
if len(data) != 0 {
that.mutex.Lock()
if data[0] == nil {
//that.mutex.Lock()
delete(that.Map, key)
//that.mutex.Unlock()
that.set()
} else {
//that.mutex.Lock()
that.Map[key] = data[0]
//that.mutex.Unlock()
that.set()
}
that.mutex.Unlock()
// 使用深拷贝版本保存,避免并发问题
that.setWithCopy()
return &Obj{Data: nil}
}
return &Obj{Data: that.Map.Get(key)}
that.mutex.RLock()
result := &Obj{Data: that.Map.Get(key)}
that.mutex.RUnlock()
return result
}
func (that *SessionIns) get() {
// getWithoutLock 内部使用,调用前需要已持有锁
func (that *SessionIns) getWithoutLock() {
that.Map = that.HoTimeCache.Session(HEAD_SESSION_ADD + that.SessionId).ToMap()
if that.Map != nil {
return
}
that.Map = Map{}
that.HoTimeCache.Session(HEAD_SESSION_ADD+that.SessionId, that.Map)
// 保存时也需要深拷贝
copyMap := make(Map, len(that.Map))
for k, v := range that.Map {
copyMap[k] = v
}
that.HoTimeCache.Session(HEAD_SESSION_ADD+that.SessionId, copyMap)
}
return
func (that *SessionIns) get() {
that.mutex.Lock()
defer that.mutex.Unlock()
that.getWithoutLock()
}
func (that *SessionIns) Init(cache *HoTimeCache) {
//that.mutex=sync.Mutex{}
that.mutex = sync.RWMutex{}
that.HoTimeCache = cache
}