WebServer添加RouterCacheExpire配置参数

This commit is contained in:
John
2018-11-24 11:55:57 +08:00
parent 4e3081afee
commit b3353afe3c
5 changed files with 22 additions and 10 deletions

View File

@ -46,8 +46,8 @@ type (
// 服务注册相关
serveTree map[string]interface{} // 所有注册的服务回调函数(路由表,树型结构,哈希表+链表优先级匹配)
hooksTree map[string]interface{} // 所有注册的事件回调函数(路由表,树型结构,哈希表+链表优先级匹配)
serveCache *gmap.StringInterfaceMap // 服务注册路由内存缓存
hooksCache *gmap.StringInterfaceMap // 事件回调路由内存缓存
serveCache *gcache.Cache // 服务注册路由内存缓存
hooksCache *gcache.Cache // 事件回调路由内存缓存
routesMap map[string][]registeredRouteItem // 已经注册的路由及对应的注册方法文件地址(用以路由重复注册判断)
// 自定义状态码回调
hsmu sync.RWMutex // status handler互斥锁
@ -185,8 +185,8 @@ func GetServer(name...interface{}) (*Server) {
statusHandlerMap : make(map[string]HandlerFunc),
serveTree : make(map[string]interface{}),
hooksTree : make(map[string]interface{}),
serveCache : gmap.NewStringInterfaceMap(),
hooksCache : gmap.NewStringInterfaceMap(),
serveCache : gcache.New(),
hooksCache : gcache.New(),
routesMap : make(map[string][]registeredRouteItem),
sessions : gcache.New(),
servedCount : gtype.NewInt(),

View File

@ -78,9 +78,10 @@ type ServerConfig struct {
NameToUriType int // 服务注册时对象和方法名称转换为URI时的规则
GzipContentTypes []string // 允许进行gzip压缩的文件类型
DumpRouteMap bool // 是否在程序启动时默认打印路由表信息
RouterCacheExpire int // 路由检索缓存过期时间(秒)
}
// 默认HTTP Server
// 默认HTTP Server配置
var defaultServerConfig = ServerConfig {
Addr : "",
HTTPSAddr : "",
@ -107,6 +108,8 @@ var defaultServerConfig = ServerConfig {
GzipContentTypes : defaultGzipContentTypes,
DumpRouteMap : true,
RouterCacheExpire : 60,
}
// 获取默认的http server设置
@ -332,6 +335,15 @@ func (s *Server) SetDumpRouteMap(enabled bool) {
s.config.DumpRouteMap = enabled
}
// 设置路由缓存过期时间(秒)
func (s *Server) SetRouterCacheExpire(expire int) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.RouterCacheExpire = expire
}
// 添加静态文件搜索目录,必须给定目录的绝对路径
func (s *Server) AddSearchPath(path string) error {
if rp, err := s.paths.Add(path); err != nil {

View File

@ -70,7 +70,7 @@ func (s *Server) setHandler(pattern string, handler *handlerItem, hook ... strin
return errors.New("invalid pattern")
}
// 注册地址记录及重复注册判断
regkey := s.hookHandlerKey(hookName, method, uri, domain)
regkey := s.handlerKey(hookName, method, uri, domain)
caller := s.getHandlerRegisterCallerLine(handler)
if len(hook) == 0 {
if item, ok := s.routesMap[regkey]; ok {

View File

@ -81,11 +81,11 @@ func (s *Server) callHookHandler(hook string, r *Request) {
// 查询请求处理方法, 带缓存机制按照Host、Method、Path进行缓存.
func (s *Server) getHookHandlerWithCache(hook string, r *Request) []*handlerParsedItem {
cacheItems := ([]*handlerParsedItem)(nil)
cacheKey := s.hookHandlerKey(hook, r.Method, r.URL.Path, r.GetHost())
cacheKey := s.handlerKey(hook, r.Method, r.URL.Path, r.GetHost())
if v := s.hooksCache.Get(cacheKey); v == nil {
cacheItems = s.searchHookHandler(r.Method, r.URL.Path, r.GetHost(), hook)
if cacheItems != nil {
s.hooksCache.Set(cacheKey, cacheItems)
s.hooksCache.Set(cacheKey, cacheItems, s.config.RouterCacheExpire*1000)
}
} else {
cacheItems = v.([]*handlerParsedItem)
@ -189,7 +189,7 @@ func (s *Server) searchHookHandler(method, path, domain, hook string) []*handler
}
// 生成hook key如果是hook key那么使用'%'符号分隔
func (s *Server) hookHandlerKey(hook, method, path, domain string) string {
func (s *Server) handlerKey(hook, method, path, domain string) string {
return hook + "%" + s.serveHandlerKey(method, path, domain)
}

View File

@ -21,7 +21,7 @@ func (s *Server) getServeHandlerWithCache(r *Request) *handlerParsedItem {
if v := s.serveCache.Get(cacheKey); v == nil {
cacheItem = s.searchServeHandler(r.Method, r.URL.Path, r.GetHost())
if cacheItem != nil {
s.serveCache.Set(cacheKey, cacheItem)
s.serveCache.Set(cacheKey, cacheItem, s.config.RouterCacheExpire*1000)
}
} else {
cacheItem = v.(*handlerParsedItem)