예제 #1
0
파일: cache.go 프로젝트: sdvdxl/gin-extend
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")
}
예제 #2
0
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,
	}
}
예제 #3
0
파일: cache.go 프로젝트: hobo-c/gin_sample
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()
	}
}
예제 #4
0
파일: cache.go 프로젝트: sdvdxl/wine
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")
}
예제 #5
0
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")
}
예제 #6
0
파일: gin.go 프로젝트: SlinSo/egon
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()
}