-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from fumiama/pkt
- Loading branch information
Showing
29 changed files
with
322 additions
and
436 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,112 @@ | ||
package cache | ||
|
||
import ( | ||
"sync" | ||
"reflect" | ||
"unsafe" | ||
|
||
"github.com/LagrangeDev/LagrangeGo/entity" | ||
"github.com/LagrangeDev/LagrangeGo/utils" | ||
"github.com/RomiChan/syncx" | ||
) | ||
|
||
var cacheLogger = utils.GetLogger("cache") | ||
|
||
type cacheType uint32 | ||
|
||
const ( | ||
cacheTypeCache cacheType = 1 << iota | ||
cacheTypeFriend | ||
cacheTypeGroupInfo | ||
cacheTypeGroupMember | ||
) | ||
|
||
func typenameof[T any]() string { | ||
return reflect.ValueOf((*T)(nil)).Type().String() | ||
} | ||
|
||
var cacheTypesMap = map[string]cacheType{ | ||
typenameof[Cache](): cacheTypeCache, | ||
typenameof[entity.Friend](): cacheTypeFriend, | ||
typenameof[entity.Group](): cacheTypeGroupInfo, | ||
typenameof[entity.GroupMember](): cacheTypeGroupMember, | ||
} | ||
|
||
type Cache struct { | ||
refreshLock sync.RWMutex | ||
// FriendCache 好友缓存 uin *entity.Friend | ||
FriendCache map[uint32]*entity.Friend | ||
// GroupInfoCache 群信息缓存 groupUin *entity.Group | ||
GroupInfoCache map[uint32]*entity.Group | ||
// GroupMemberCache 群内群员信息缓存 groupUin uin *entity.GroupMember | ||
GroupMemberCache map[uint32]map[uint32]*entity.GroupMember | ||
m syncx.Map[uint64, unsafe.Pointer] | ||
refreshed syncx.Map[cacheType, struct{}] | ||
} | ||
|
||
func hasRefreshed[T any](c *Cache) bool { | ||
typ := cacheTypesMap[reflect.ValueOf((*T)(nil)).Type().String()] | ||
if typ == 0 { | ||
return false | ||
} | ||
_, ok := c.refreshed.Load(typ) | ||
return ok | ||
} | ||
|
||
func refreshAllCacheOf[T any](c *Cache, newcache map[uint32]*T) { | ||
typstr := reflect.ValueOf((*T)(nil)).Type().String() | ||
typ := cacheTypesMap[typstr] | ||
if typ == 0 { | ||
return | ||
} | ||
cacheLogger.Debugln("refresh all cache of", typstr) | ||
c.refreshed.Store(typ, struct{}{}) | ||
key := uint64(typ) << 32 | ||
dellst := make([]uint64, 0, 64) | ||
c.m.Range(func(k uint64, v unsafe.Pointer) bool { | ||
if k&key != 0 { | ||
if _, ok := newcache[uint32(k)]; !ok { | ||
dellst = append(dellst, k) | ||
} | ||
} | ||
return true | ||
}) | ||
for k, v := range newcache { | ||
c.m.Store(key|uint64(k), unsafe.Pointer(v)) | ||
} | ||
for _, k := range dellst { | ||
c.m.Delete(k) | ||
} | ||
} | ||
|
||
func setCacheOf[T any](c *Cache, k uint32, v *T) { | ||
typstr := reflect.ValueOf(v).Type().String() | ||
typ := cacheTypesMap[typstr] | ||
if typ == 0 { | ||
return | ||
} | ||
key := uint64(typ)<<32 | uint64(k) | ||
c.m.Store(key, unsafe.Pointer(v)) | ||
cacheLogger.Debugln("set cache of", typstr, k, "=", v) | ||
} | ||
|
||
func getCacheOf[T any](c *Cache, k uint32) (v *T, ok bool) { | ||
typstr := reflect.ValueOf(v).Type().String() | ||
typ := cacheTypesMap[typstr] | ||
if typ == 0 { | ||
return | ||
} | ||
key := uint64(typ)<<32 | uint64(k) | ||
unsafev, ok := c.m.Load(key) | ||
if ok { | ||
v = (*T)(unsafev) | ||
} | ||
cacheLogger.Debugln("get cache of", typstr, k, "=", v) | ||
return | ||
} | ||
|
||
func NewCache() *Cache { | ||
return &Cache{ | ||
FriendCache: make(map[uint32]*entity.Friend), | ||
GroupInfoCache: make(map[uint32]*entity.Group), | ||
GroupMemberCache: make(map[uint32]map[uint32]*entity.GroupMember), | ||
func rangeCacheOf[T any](c *Cache, iter func(k uint32, v *T) bool) { | ||
typ := cacheTypesMap[reflect.ValueOf((*T)(nil)).Type().String()] | ||
if typ == 0 { | ||
return | ||
} | ||
key := uint64(typ) << 32 | ||
c.m.Range(func(k uint64, v unsafe.Pointer) bool { | ||
if k&key != 0 { | ||
return iter(uint32(k), (*T)(v)) | ||
} | ||
return true | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.