mirror of
https://github.com/weloe/token-go.git
synced 2025-10-05 15:36:50 +08:00
feat: add GetLoginCounts, GetLoginTokenCounts
This commit is contained in:
24
enforcer.go
24
enforcer.go
@@ -533,3 +533,27 @@ func (e *Enforcer) UpdateSession(id string, session *model.Session) error {
|
||||
func (e *Enforcer) GetTokenConfig() config.TokenConfig {
|
||||
return e.config
|
||||
}
|
||||
|
||||
func (e *Enforcer) GetLoginCounts() (int, error) {
|
||||
adapter, ok := e.adapter.(persist.BatchAdapter)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("the adapter does not implement persist.BatchAdapter")
|
||||
}
|
||||
c, err := adapter.GetCountsFilteredKey(e.spliceSessionKey(""))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (e *Enforcer) GetLoginTokenCounts() (int, error) {
|
||||
adapter, ok := e.adapter.(persist.BatchAdapter)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("the adapter does not implement persist.BatchAdapter")
|
||||
}
|
||||
c, err := adapter.GetCountsFilteredKey(e.spliceTokenKey(""))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
@@ -45,6 +45,9 @@ type IEnforcer interface {
|
||||
GetIdByToken(token string) string
|
||||
GetLoginCount(id string) int
|
||||
|
||||
GetLoginCounts() (int, error)
|
||||
GetLoginTokenCounts() (int, error)
|
||||
|
||||
Kickout(id string, device string) error
|
||||
Replaced(id string, device string) error
|
||||
|
||||
|
@@ -4,4 +4,6 @@ type BatchAdapter interface {
|
||||
Adapter
|
||||
// DeleteBatchFilteredKey delete data by keyPrefix
|
||||
DeleteBatchFilteredKey(filterKeyPrefix string) error
|
||||
// GetCountsFilteredKey get data by keyPrefix
|
||||
GetCountsFilteredKey(filterKeyPrefix string) (int, error)
|
||||
}
|
||||
|
@@ -135,6 +135,21 @@ func (d *DefaultAdapter) DeleteBatchFilteredKey(keyPrefix string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *DefaultAdapter) GetCountsFilteredKey(keyPrefix string) (int, error) {
|
||||
cacheEx, ok := d.cache.(cache.CacheEx)
|
||||
if !ok {
|
||||
return 0, errors.New("the cache does not implement the Range method")
|
||||
}
|
||||
var counts int
|
||||
cacheEx.Range(func(key, value any) bool {
|
||||
if strings.HasPrefix(key.(string), keyPrefix) {
|
||||
counts++
|
||||
}
|
||||
return true
|
||||
})
|
||||
return counts, nil
|
||||
}
|
||||
|
||||
func (d *DefaultAdapter) EnableCleanTimer(b bool) {
|
||||
d.enableRefreshTimer = b
|
||||
}
|
||||
|
Reference in New Issue
Block a user