GoFrame Session-Storage接口開發(fā)

2022-04-14 11:58 更新

大部分場景下,通過?gsession?組件內(nèi)置提供的常見?Storage?實現(xiàn)已經(jīng)能夠滿足需求。如果有特殊的場景需要制定不易開發(fā)?Storage?當(dāng)然也是支持的,因為?gsession?的功能都采用了接口化設(shè)計。

Storage定義

https://github.com/gogf/gf/v2/blob/master/os/gsession/gsession_storage.go

// Storage is the interface definition for session storage.
type Storage interface {
	// New creates a custom session id.
	// This function can be used for custom session creation.
	New(ctx context.Context, ttl time.Duration) (id string, err error)

	// Get retrieves and returns session value with given key.
	// It returns nil if the key does not exist in the session.
	Get(ctx context.Context, id string, key string) (value interface{}, err error)

	// GetMap retrieves all key-value pairs as map from storage.
	GetMap(ctx context.Context, id string) (data map[string]interface{}, err error)

	// GetSize retrieves and returns the size of key-value pairs from storage.
	GetSize(ctx context.Context, id string) (size int, err error)

	// Set sets one key-value session pair to the storage.
	// The parameter `ttl` specifies the TTL for the session id.
	Set(ctx context.Context, id string, key string, value interface{}, ttl time.Duration) error

	// SetMap batch sets key-value session pairs as map to the storage.
	// The parameter `ttl` specifies the TTL for the session id.
	SetMap(ctx context.Context, id string, data map[string]interface{}, ttl time.Duration) error

	// Remove deletes key with its value from storage.
	Remove(ctx context.Context, id string, key string) error

	// RemoveAll deletes all key-value pairs from storage.
	RemoveAll(ctx context.Context, id string) error

	// GetSession returns the session data as `*gmap.StrAnyMap` for given session id from storage.
	//
	// The parameter `ttl` specifies the TTL for this session.
	// The parameter `data` is the current old session data stored in memory,
	// and for some storage it might be nil if memory storage is disabled.
	//
	// This function is called ever when session starts. It returns nil if the TTL is exceeded.
	GetSession(ctx context.Context, id string, ttl time.Duration, data *gmap.StrAnyMap) (*gmap.StrAnyMap, error)

	// SetSession updates the data for specified session id.
	// This function is called ever after session, which is changed dirty, is closed.
	// This copy all session data map from memory to storage.
	SetSession(ctx context.Context, id string, data *gmap.StrAnyMap, ttl time.Duration) error

	// UpdateTTL updates the TTL for specified session id.
	// This function is called ever after session, which is not dirty, is closed.
	UpdateTTL(ctx context.Context, id string, ttl time.Duration) error
}

每一個方法的調(diào)用時機(jī)都在注釋中詳細(xì)介紹了,開發(fā)者在實現(xiàn)自定義的?Storage?時,可以充分參考內(nèi)置的幾種?Storage?實現(xiàn)。

注意事項

?Storage?接口中,并不是所有的接口方法都需要實現(xiàn),開發(fā)者僅需要根據(jù)業(yè)務(wù)需要,實現(xiàn)特定調(diào)用時機(jī)的一些接口即可。

為了提高?Session?的執(zhí)行性能,接口有?gmap.StrAnyMap?容器類型的使用。


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號