func init() { Logger.Info("init cache ...") Cache = cache.NewRedisCache(config.AuthPageConfig.RedisHost, config.AuthPageConfig.RedisPassword, constant.LOGIN_EXPIRED_TIME) if err := Cache.Set("_test_is_valid_x_", "x", time.Second); err != nil { Logger.Error("error redis status,%v, will use memcached", err) Cache = cache.NewMemcachedStore(config.AuthPageConfig.MemcachedHosts, constant.LOGIN_EXPIRED_TIME) if err = Cache.Set("_test_is_valid_x_", "x", time.Second); err != nil { Logger.Error("error redis status,%v, will use memry cache", err) Cache = cache.NewInMemoryStore(constant.LOGIN_EXPIRED_TIME) } } Logger.Info("cache inited") }
func NewServer(conn *grpc.ClientConn, secretKey string, debug bool) *Server { c := pb.NewApiClient(conn) worker := &pb.Worker{ Id: randhash(), } httpclient := &http.Client{ Timeout: 30 * time.Second, } cacheStore := cache.NewInMemoryStore(time.Second) rc, _ := react.NewReact() jsx, _ := react.NewJSX() // TODO: caching if we have more components if debug { component, err := jsx.TransformFile("./templates/_feed.jsx", map[string]interface{}{ "harmony": true, "strip_types": true, }) if err != nil { panic(err) } err = rc.Load(component) if err != nil { panic(err) } } else { component, err := Asset("static/js/bundle.min.js") if err != nil { panic(err) } err = rc.Load(component) if err != nil { panic(err) } } return &Server{ debug: debug, client: c, worker: worker, secretKey: secretKey, httpclient: httpclient, cache: cacheStore, jsx: jsx, rc: rc, } }
func Cache() gin.HandlerFunc { var store gin_cache.CacheStore switch conf.CACHE_STORE { case conf.MEMCACHED: store = gin_cache.NewMemcachedStore([]string{conf.MEMCACHED_SERVER}, time.Hour) default: store = gin_cache.NewInMemoryStore(time.Hour) } return func(c *gin.Context) { c.Set(DefaultKey, store) c.Next() } }
func init() { log.Logger.Info("init cache ...") // Cache = cache.NewRedisCache("redis-31pzn.q1.tenxcloud.net:48181", "Isdvdxl8", constant.LOGIN_EXPIRED_TIME) Cache = cache.NewRedisCache("54.223.168.107:61993", "Isdvdxl8", constant.LOGIN_EXPIRED_TIME) if err := Cache.Set("_test_is_valid_x_", "x", time.Second); err != nil { log.Logger.Error("error redis status,%v, will use memcached", err) Cache = cache.NewMemcachedStore([]string{}, constant.LOGIN_EXPIRED_TIME) if err = Cache.Set("_test_is_valid_x_", "x", time.Second); err != nil { log.Logger.Error("error redis status,%v, will use memry cache", err) Cache = cache.NewInMemoryStore(constant.LOGIN_EXPIRED_TIME) } } log.Logger.Info("cache inited") }
func main() { r := gin.Default() store := cache.NewInMemoryStore(time.Second) // Cached Page r.GET("/ping", func(c *gin.Context) { c.String(200, "pong "+fmt.Sprint(time.Now().Unix())) }) r.GET("/cache_ping", cache.CachePage(store, time.Minute, func(c *gin.Context) { c.String(200, "pong "+fmt.Sprint(time.Now().Unix())) })) // Listen and Server in 0.0.0.0:8080 r.Run(":8080") }
func mainGin() { r := gin.Default() r.GET("/hello", func(c *gin.Context) { u := &model.User{} c.Status(200) c.Writer.Header()["Content-Type"] = []string{"text/html; charset=utf-8"} SimpleTemplate(c.Writer, u) }) // caching can still happen in middleware store := cache.NewInMemoryStore(time.Second) r.GET("/hello_cache", cache.CachePage(store, time.Minute, func(c *gin.Context) { u := &model.User{} c.Status(200) c.Writer.Header()["Content-Type"] = []string{"text/html; charset=utf-8"} SimpleTemplate(c.Writer, u) })) r.Run() }