完善 cache 相关内容

This commit is contained in:
dragon
2025-01-03 17:25:41 +08:00
parent 6fcf929331
commit 246bf13a6c
15 changed files with 377 additions and 102 deletions

View File

@@ -1,24 +1,53 @@
package pkg
import (
"code.d7z.net/d7z-project/gitea-pages/pkg/services"
"net/http"
"time"
"code.d7z.net/d7z-project/gitea-pages/pkg/core"
"code.d7z.net/d7z-project/gitea-pages/pkg/utils"
"github.com/pbnjay/memory"
)
type ServerOptions struct {
Domain string
Cache services.Config
Config utils.Config
Cache utils.Cache
MaxCacheSize int
}
func DefaultOptions(domain string) *ServerOptions {
return &ServerOptions{
Domain: domain,
Cache: services.NewConfigMemory(),
func DefaultOptions(domain string) ServerOptions {
configMemory, _ := utils.NewConfigMemory("")
return ServerOptions{
Domain: domain,
Config: configMemory,
Cache: utils.NewCacheMemory(1024*1024*10, int(memory.FreeMemory()/3*2)),
MaxCacheSize: 1024 * 1024 * 10,
}
}
type Server struct {
backend core.Backend
options *ServerOptions
}
func NewServer(backend services.Backend, options *ServerOptions) *Server {
func NewPageServer(backend core.Backend, options ServerOptions) *Server {
return &Server{
backend: core.NewCacheBackend(backend, options.Config, time.Minute),
options: &options,
}
}
func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
}
func (s *Server) Close() error {
if err := s.options.Config.Close(); err != nil {
return err
}
if err := s.options.Cache.Close(); err != nil {
return err
}
return nil
}