cache

package
v1.13.0-alpha.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2022 License: Apache-2.0, BSD-2-Clause, BSD-3-Clause, + 3 more Imports: 26 Imported by: 6

Documentation

Index

Constants

View Source
const (
	// CacheNamespace int = iota
	// CacheBusiness
	CacheService = iota
	CacheInstance
	CacheRoutingConfig
	CacheCL5
	CacheRateLimit
	CacheCircuitBreaker
	CacheUser
	CacheAuthStrategy
	CacheNamespace
	CacheClient
	CacheConfigFile

	CacheLast
)
View Source
const (
	// RevisionConcurrenceCount Revision计算的并发线程数
	RevisionConcurrenceCount = 64
	// RevisionChanCount 存储revision计算的通知管道,可以稍微设置大一点
	RevisionChanCount = 102400
)
View Source
const (
	UsersName = "users"

	NameLinkOwnerTemp = "%s@%s"
)
View Source
const (
	// CircuitBreakerName circuit breaker config name
	CircuitBreakerName = "circuitBreakerConfig"
)
View Source
const (
	// ClientName client cache name
	ClientName = "client"
)
View Source
const (
	// DefaultTimeDiff default time diff
	DefaultTimeDiff = -10 * time.Second
)
View Source
const (
	// InstanceName instance name
	InstanceName = "instance"
)
View Source
const (
	// L5Name l5 name
	L5Name = "l5"
)
View Source
const (
	// NamespaceName l5 name
	NamespaceName = "namespace"
)
View Source
const (
	// RateLimitConfigName rate limit config name
	RateLimitConfigName = "rateLimitConfig"
)
View Source
const (
	// RoutingConfigName router config name
	RoutingConfigName = "routingConfig"
)
View Source
const (
	ServiceName = "service"
)
View Source
const (
	StrategyRuleName = "strategyRule"
)
View Source
const (
	// UpdateCacheInterval 缓存更新时间间隔
	UpdateCacheInterval = 1 * time.Second
)

Variables

View Source
var (
	TestCacheInitialize = func(ctx context.Context, cacheOpt *Config, storage store.Store) (*CacheManager, error) {
		mgr, err := newCacheManager(ctx, cacheOpt, storage)
		if err := Run(mgr, ctx); err != nil {
			return nil, err
		}
		return mgr, err
	}
)

TestCacheInitialize 由于某一些模块依赖Cache,但是由于Cache模块初始化采用sync.Once,会导致单元测试之间Cache存在脏数据问题,因此为了确保单 元测试能够正常执行,这里将 cache.initialize 方法导出并命名为 TestCacheInitialize,仅用于单元测试初始化一个完整的 CacheManager

Functions

func CompositeComputeRevision

func CompositeComputeRevision(revisions []string) (string, error)

CompositeComputeRevision 将多个 revision 合并计算为一个

func ComputeRevision

func ComputeRevision(serviceRevision string, instances []*model.Instance) (string, error)

ComputeRevision 计算唯一的版本标识

func Initialize

func Initialize(ctx context.Context, cacheOpt *Config, storage store.Store) error

Initialize 初始化

func RegisterCache

func RegisterCache(name string, index int)

RegisterCache 注册缓存资源

func Run

func Run(cacheMgr *CacheManager, ctx context.Context) error

func SetCacheConfig

func SetCacheConfig(conf *Config)

SetCacheConfig 设置缓存配置

Types

type Args

type Args struct {
	// StoreTimeRollbackSec 存储层时钟回拨情况
	StoreTimeRollbackSec time.Duration
}

type Cache

type Cache interface {
	// contains filtered or unexported methods
}

Cache 缓存接口

type CacheManager

type CacheManager struct {
	// contains filtered or unexported fields
}

CacheManager 名字服务缓存

func GetCacheManager

func GetCacheManager() (*CacheManager, error)

GetCacheManager

@return *CacheManager
@return error

func (*CacheManager) AddListener

func (nc *CacheManager) AddListener(cacheName CacheName, listeners []Listener)

func (*CacheManager) AuthStrategy

func (nc *CacheManager) AuthStrategy() StrategyCache

AuthStrategy Get authentication cache information

func (*CacheManager) CL5

func (nc *CacheManager) CL5() L5Cache

CL5 获取l5缓存信息

func (*CacheManager) CircuitBreaker

func (nc *CacheManager) CircuitBreaker() CircuitBreakerCache

CircuitBreaker 获取熔断规则缓存信息

func (*CacheManager) Clear

func (nc *CacheManager) Clear() error

Clear 主动清除缓存数据

func (*CacheManager) Client

func (nc *CacheManager) Client() ClientCache

Client Get client cache information

func (*CacheManager) ConfigFile

func (nc *CacheManager) ConfigFile() FileCache

ConfigFile get config file cache information

func (*CacheManager) GetServiceInstanceRevision

func (nc *CacheManager) GetServiceInstanceRevision(serviceID string) string

GetServiceInstanceRevision 获取服务实例计算之后的revision

func (*CacheManager) GetServiceRevisionCount

func (nc *CacheManager) GetServiceRevisionCount() int

GetServiceRevisionCount 计算一下缓存中的revision的个数

func (*CacheManager) GetStore

func (nc *CacheManager) GetStore() store.Store

GetStore get store

func (*CacheManager) GetUpdateCacheInterval

func (nc *CacheManager) GetUpdateCacheInterval() time.Duration

GetUpdateCacheInterval 获取当前cache的更新间隔

func (*CacheManager) Instance

func (nc *CacheManager) Instance() InstanceCache

Instance 获取Instance缓存信息

func (*CacheManager) Namespace

func (nc *CacheManager) Namespace() NamespaceCache

Namespace Get namespace cache information

func (*CacheManager) RateLimit

func (nc *CacheManager) RateLimit() RateLimitCache

RateLimit 获取限流规则缓存信息

func (*CacheManager) RoutingConfig

func (nc *CacheManager) RoutingConfig() RoutingConfigCache

RoutingConfig 获取路由配置的缓存信息

func (*CacheManager) Service

func (nc *CacheManager) Service() ServiceCache

Service 获取Service缓存信息

func (*CacheManager) Start

func (nc *CacheManager) Start(ctx context.Context) error

Start 缓存对象启动协程,定时更新缓存

func (*CacheManager) TestRefresh

func (nc *CacheManager) TestRefresh() error

TestRefresh only for test

func (*CacheManager) User

func (nc *CacheManager) User() UserCache

User Get user information cache information

type CacheName

type CacheName string

CacheName cache name

const (
	CacheNameService        CacheName = "Service"
	CacheNameInstance       CacheName = "Instance"
	CacheNameRoutingConfig  CacheName = "RoutingConfig"
	CacheNameCL5            CacheName = "CL5"
	CacheNameRateLimit      CacheName = "RateLimit"
	CacheNameCircuitBreaker CacheName = "CircuitBreaker"
	CacheNameUser           CacheName = "User"
	CacheNameAuthStrategy   CacheName = "AuthStrategy"
	CacheNameNamespace      CacheName = "Namespace"
	CacheNameClient         CacheName = "Client"
	CacheNameConfigFile     CacheName = "ConfigFile"
)

type CircuitBreakerCache

type CircuitBreakerCache interface {
	Cache

	// GetCircuitBreakerConfig 根据ServiceID获取熔断配置
	GetCircuitBreakerConfig(id string) *model.ServiceWithCircuitBreaker
}

CircuitBreakerCache circuitBreaker配置的cache接口

type ClientCache

type ClientCache interface {
	Cache

	// GetClient get client
	GetClient(id string) *model.Client

	// IteratorClients 迭代
	IteratorClients(iterProc ClientIterProc)

	// GetClientsByFilter Query client information
	GetClientsByFilter(filters map[string]string, offset, limit uint32) (uint32, []*model.Client, error)
}

ClientCache 客户端的 Cache 接口

type ClientIterProc

type ClientIterProc func(key string, value *model.Client) bool

ClientIterProc client iter proc func

type Config

type Config struct {
	Open      bool `yaml:"open"`
	Resources []ConfigEntry
}

Config 缓存配置

type ConfigEntry

type ConfigEntry struct {
	Name   string                 `yaml:"name"`
	Option map[string]interface{} `yaml:"option"`
}

ConfigEntry 单个缓存资源配置

type Entry

type Entry struct {
	Content string
	Md5     string
	Version uint64
	// 创建的时候,设置过期时间
	ExpireTime time.Time
	// 标识是否是空缓存
	Empty bool
}

Entry 缓存实体对象

type EventType

type EventType int

EventType common event type

const (
	// EventCreated value create event
	EventCreated EventType = iota
	// EventUpdated value update event
	EventUpdated
	// EventDeleted value delete event
	EventDeleted
	// EventInstanceReload value instances reload
	EventInstanceReload
	// EventPrincipalRemove value principal batch remove
	EventPrincipalRemove
)

type FileCache

type FileCache interface {
	Cache
	// Get 通过ns,group,filename获取 Entry
	Get(namespace, group, fileName string) (*Entry, bool)
	// GetOrLoadIfAbsent
	GetOrLoadIfAbsent(namespace, group, fileName string) (*Entry, error)
	// Remove
	Remove(namespace, group, fileName string)
	// ReLoad
	ReLoad(namespace, group, fileName string) (*Entry, error)
	// GetOrLoadGroupByName
	GetOrLoadGroupByName(namespace, group string) (*model.ConfigFileGroup, error)
	// GetOrLoadGroupById
	GetOrLoadGroupById(id uint64) (*model.ConfigFileGroup, error)
	// CleanAll
	CleanAll()
}

FileCache file cache

type InstanceCache

type InstanceCache interface {
	// Cache 公共缓存接口
	Cache
	// GetInstance 根据实例ID获取实例数据
	GetInstance(instanceID string) *model.Instance
	// GetInstancesByServiceID 根据服务名获取实例,先查找服务名对应的服务ID,再找实例列表
	GetInstancesByServiceID(serviceID string) []*model.Instance
	// IteratorInstances 迭代
	IteratorInstances(iterProc InstanceIterProc) error
	// IteratorInstancesWithService 根据服务ID进行迭代
	IteratorInstancesWithService(serviceID string, iterProc InstanceIterProc) error
	// GetInstancesCount 获取instance的个数
	GetInstancesCount() int
	// GetInstancesCountByServiceID 根据服务ID获取实例数
	GetInstancesCountByServiceID(serviceID string) model.InstanceCount
	// GetServicePorts 根据服务ID获取端口号
	GetServicePorts(serviceID string) []string
	// GetInstanceLabels Get the label of all instances under a service
	GetInstanceLabels(serviceID string) *api.InstanceLabels
}

InstanceCache 实例相关的缓存接口

type InstanceIterProc

type InstanceIterProc func(key string, value *model.Instance) (bool, error)

InstanceIterProc instance iter proc func

type L5Cache

type L5Cache interface {
	Cache

	// GetRouteByIP 根据IP获取访问关系
	GetRouteByIP(ip uint32) []*model.Route

	// CheckRouteExisted 检查IP对应的SID是否存在访问关系
	CheckRouteExisted(ip uint32, modID uint32, cmdID uint32) bool

	// GetPolicy 获取有状态路由信息policy
	GetPolicy(modID uint32) *model.Policy

	// GetSection 获取有状态路由信息policy
	GetSection(modeID uint32) []*model.Section

	// GetIPConfig 获取IpConfig
	GetIPConfig(ip uint32) *model.IPConfig
}

L5Cache L5的cache接口

type Listener

type Listener interface {
	// OnCreated callback when cache value created
	OnCreated(value interface{})
	// OnUpdated callback when cache value updated
	OnUpdated(value interface{})
	// OnDeleted callback when cache value deleted
	OnDeleted(value interface{})
	// OnBatchCreated callback when cache value created
	OnBatchCreated(value interface{})
	// OnBatchUpdated callback when cache value updated
	OnBatchUpdated(value interface{})
	// OnBatchDeleted callback when cache value deleted
	OnBatchDeleted(value interface{})
}

Listener listener for value changes

type NamespaceCache

type NamespaceCache interface {
	Cache

	// GetNamespace
	//  @param id
	//  @return *model.Namespace
	GetNamespace(id string) *model.Namespace

	// GetNamespacesByName
	//  @param names
	//  @return []*model.Namespace
	//  @return error
	GetNamespacesByName(names []string) []*model.Namespace

	// GetNamespaceList
	//  @return []*model.Namespace
	GetNamespaceList() []*model.Namespace
}

NamespaceCache 命名空间的 Cache 接口

type RateLimitCache

type RateLimitCache interface {
	Cache

	// GetRateLimit 根据serviceID进行迭代回调
	GetRateLimit(serviceID string, rateLimitIterProc RateLimitIterProc) error

	// GetLastRevision 根据serviceID获取最新revision
	GetLastRevision(serviceID string) string

	// GetRateLimitByServiceID 根据serviceID获取限流数据
	GetRateLimitByServiceID(serviceID string) []*model.RateLimit

	// GetRevisionsCount 获取revision总数
	GetRevisionsCount() int

	// GetRateLimitsCount 获取限流规则总数
	GetRateLimitsCount() int
}

RateLimitCache rateLimit的cache接口

type RateLimitIterProc

type RateLimitIterProc func(id string, rateLimit *model.RateLimit) (bool, error)

RateLimitIterProc rate limit iter func

type RoutingArgs

type RoutingArgs struct {
	// Filter
	Filter map[string]string
	// ID 路由规则 ID
	ID string
	// Name 条件中的服务名
	Name string
	// Service 主调 or 被调服务名称
	Service string
	// Namespace 主调 or 被调服务所在命名空间
	Namespace string
	// SourceService 主调服务
	SourceService string
	// SourceNamespace 主调服务所在命名空间
	SourceNamespace string
	// DestinationService 被调服务
	DestinationService string
	// DestinationNamespace 被调服务所在命名空间
	DestinationNamespace string
	// Enable
	Enable *bool
	// Offset
	Offset uint32
	// Limit
	Limit uint32
	// OrderField 排序字段
	OrderField string
	// OrderType 排序规则
	OrderType string
}

RoutingArgs 路由规则查询参数

type RoutingConfigCache

type RoutingConfigCache interface {
	Cache
	// GetRoutingConfigV1 根据ServiceID获取路由配置
	GetRoutingConfigV1(id, service, namespace string) (*apiv1.Routing, error)
	// GetRoutingConfig 根据ServiceID获取路由配置
	GetRoutingConfigV2(id, service, namespace string) ([]*apiv2.Routing, error)
	// GetRoutingConfigCount 获取路由配置缓存的总个数
	GetRoutingConfigCount() int
	// GetRoutingConfigsV2 查询路由配置列表
	GetRoutingConfigsV2(args *RoutingArgs) (uint32, []*v2.ExtendRoutingConfig, error)
	// IsConvertFromV1 当前路由规则是否是从 v1 规则转换过来的
	IsConvertFromV1(id string) (string, bool)
}

RoutingConfigCache routing配置的cache接口

type RoutingIterProc

type RoutingIterProc func(key string, value *v2.ExtendRoutingConfig)

RoutingIterProc 遍历路由规则的方法定义

type ServiceArgs

type ServiceArgs struct {
	// Filter 普通服务字段条件
	Filter map[string]string
	// Metadata 元数据条件
	Metadata map[string]string
	// SvcIds 是否按照服务的ID进行等值查询
	SvcIds map[string]struct{}
	// FuzzyName 是否进行名字的模糊匹配
	FuzzyName bool
	// FuzzyBusiness 是否进行业务的模糊匹配
	FuzzyBusiness bool
	// Namespace 条件中的命名空间
	Namespace string
	// Name 条件中的服务名
	Name string
	// EmptyCondition 是否是空条件,即只需要从所有服务或者某个命名空间下面的服务,进行不需要匹配的遍历,返回前面的服务即可
	EmptyCondition bool
	// HiddenServiceSet 需要隐藏的服务
	HiddenServiceSet map[model.ServiceKey]struct{}
}

ServiceArgs 服务查询条件

type ServiceCache

type ServiceCache interface {
	Cache

	// GetNamespaceCntInfo Return to the service statistics according to the namespace,
	// 	the count statistics and health instance statistics
	GetNamespaceCntInfo(namespace string) model.NamespaceServiceCount

	// GetAllNamespaces Return all namespaces
	GetAllNamespaces() []string

	// GetServiceByID According to ID query service information
	GetServiceByID(id string) *model.Service

	// GetServiceByName Inquiry service information according to service name
	GetServiceByName(name string, namespace string) *model.Service

	// IteratorServices Iterative Cache Service Information
	IteratorServices(iterProc ServiceIterProc) error

	// CleanNamespace Clear the cache of NameSpace
	CleanNamespace(namespace string)

	// GetServicesCount Get the number of services in the cache
	GetServicesCount() int

	// GetServiceByCl5Name Get the corresponding SID according to CL5name
	GetServiceByCl5Name(cl5Name string) *model.Service

	// GetServicesByFilter Serving the service filtering in the cache through Filter
	GetServicesByFilter(serviceFilters *ServiceArgs,
		instanceFilters *store.InstanceArgs, offset, limit uint32) (uint32, []*model.EnhancedService, error)

	// Update Query trigger update interface
	Update() error
}

ServiceCache 服务数据缓存接口

type ServiceIterProc

type ServiceIterProc func(key string, value *model.Service) (bool, error)

ServiceIterProc 迭代回调函数

type StrategyCache

type StrategyCache interface {
	Cache

	// GetStrategyDetailsByUID
	//  @param uid
	//  @return []*model.StrategyDetail
	GetStrategyDetailsByUID(uid string) []*model.StrategyDetail

	// GetStrategyDetailsByGroupID returns all strategy details of a group.
	GetStrategyDetailsByGroupID(groupId string) []*model.StrategyDetail

	// IsResourceLinkStrategy 该资源是否关联了鉴权策略
	IsResourceLinkStrategy(resType api.ResourceType, resId string) bool

	// IsResourceEditable 判断该资源是否可以操作
	IsResourceEditable(principal model.Principal, resType api.ResourceType, resId string) bool
}

StrategyCache is a cache for strategy rules.

type UserCache

type UserCache interface {
	Cache

	// GetAdmin 获取管理员信息
	GetAdmin() *model.User

	// GetUserByID
	//  @param id
	//  @return *model.User
	GetUserByID(id string) *model.User

	// GetUserByName
	//  @param name
	//  @return *model.User
	GetUserByName(name, ownerName string) *model.User

	// GetUserGroup
	//  @param id
	//  @return *model.UserGroupDetail
	GetGroup(id string) *model.UserGroupDetail

	// IsUserInGroup 判断 userid 是否在对应的 group 中
	//  @param userId
	//  @param groupId
	//  @return bool
	IsUserInGroup(userId, groupId string) bool

	// IsOwner
	//  @param id
	//  @return bool
	IsOwner(id string) bool

	// ListUserBelongGroupIDS
	//  @param id
	//  @return []string
	GetUserLinkGroupIds(id string) []string
}

UserCache User information cache

type WatchInstanceReload

type WatchInstanceReload struct {
	// 实际的处理方法
	Handler func(val interface{})
}

WatchInstanceReload Listener 的一个简单实现

func (*WatchInstanceReload) OnBatchCreated

func (fc *WatchInstanceReload) OnBatchCreated(value interface{})

OnBatchCreated callback when cache value created

func (*WatchInstanceReload) OnBatchDeleted

func (fc *WatchInstanceReload) OnBatchDeleted(value interface{})

OnBatchDeleted callback when cache value deleted

func (*WatchInstanceReload) OnBatchUpdated

func (fc *WatchInstanceReload) OnBatchUpdated(value interface{})

OnBatchUpdated callback when cache value updated

func (*WatchInstanceReload) OnCreated

func (fc *WatchInstanceReload) OnCreated(value interface{})

OnCreated callback when cache value created

func (*WatchInstanceReload) OnDeleted

func (fc *WatchInstanceReload) OnDeleted(value interface{})

OnDeleted callback when cache value deleted

func (*WatchInstanceReload) OnUpdated

func (fc *WatchInstanceReload) OnUpdated(value interface{})

OnUpdated callback when cache value updated

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL