feat: add GetLoginCounts, GetLoginTokenCounts

This commit is contained in:
weloe
2023-10-21 23:04:58 +08:00
parent 5fc3db66a1
commit 2f0bfb361d
4 changed files with 44 additions and 0 deletions

View File

@@ -533,3 +533,27 @@ func (e *Enforcer) UpdateSession(id string, session *model.Session) error {
func (e *Enforcer) GetTokenConfig() config.TokenConfig { func (e *Enforcer) GetTokenConfig() config.TokenConfig {
return e.config 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
}

View File

@@ -45,6 +45,9 @@ type IEnforcer interface {
GetIdByToken(token string) string GetIdByToken(token string) string
GetLoginCount(id string) int GetLoginCount(id string) int
GetLoginCounts() (int, error)
GetLoginTokenCounts() (int, error)
Kickout(id string, device string) error Kickout(id string, device string) error
Replaced(id string, device string) error Replaced(id string, device string) error

View File

@@ -4,4 +4,6 @@ type BatchAdapter interface {
Adapter Adapter
// DeleteBatchFilteredKey delete data by keyPrefix // DeleteBatchFilteredKey delete data by keyPrefix
DeleteBatchFilteredKey(filterKeyPrefix string) error DeleteBatchFilteredKey(filterKeyPrefix string) error
// GetCountsFilteredKey get data by keyPrefix
GetCountsFilteredKey(filterKeyPrefix string) (int, error)
} }

View File

@@ -135,6 +135,21 @@ func (d *DefaultAdapter) DeleteBatchFilteredKey(keyPrefix string) error {
return err 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) { func (d *DefaultAdapter) EnableCleanTimer(b bool) {
d.enableRefreshTimer = b d.enableRefreshTimer = b
} }