コード例 #1
0
ファイル: gomemc_store.go プロジェクト: jrconlin/pushgo
// Init initializes the memcached adapter with the given configuration.
// Implements HasConfigStruct.Init().
func (s *GomemcStore) Init(app *Application, config interface{}) (err error) {
	conf := config.(*GomemcConf)
	s.logger = app.Logger()
	s.defaultHost = app.Hostname()
	s.maxChannels = conf.MaxChannels

	if len(conf.ElastiCacheConfigEndpoint) == 0 {
		s.Hosts = conf.Driver.Hosts
	} else {
		endpoints, err := GetElastiCacheEndpointsTimeout(conf.ElastiCacheConfigEndpoint, 2*time.Second)
		if err != nil {
			s.logger.Panic("storage", "Failed to retrieve ElastiCache nodes",
				LogFields{"error": err.Error()})
			return err
		}
		s.Hosts = endpoints
	}

	serverList := new(mc.ServerList)
	if err = serverList.SetServers(s.Hosts...); err != nil {
		s.logger.Panic("gomemc", "Failed to set server host list",
			LogFields{"error": err.Error()})
		return err
	}

	s.PingPrefix = conf.Db.PingPrefix

	if s.HandleTimeout, err = time.ParseDuration(conf.Db.HandleTimeout); err != nil {
		s.logger.Panic("gomemc", "Db.HandleTimeout must be a valid duration",
			LogFields{"error": err.Error()})
		return err
	}

	s.TimeoutLive = time.Duration(conf.Db.TimeoutLive) * time.Second
	s.TimeoutReg = time.Duration(conf.Db.TimeoutReg) * time.Second
	s.TimeoutDel = time.Duration(conf.Db.TimeoutReg) * time.Second

	s.client = mc.NewFromSelector(serverList)
	s.client.Timeout = s.HandleTimeout

	return nil
}
コード例 #2
0
// Create a new Client.
// Config must have the servers array.
// Optional the defaultExpiration.
func (c *MemcacheClient) ConnectClient(servers []string, defaultExpiration ...int32) (err error) {

	selector := new(memcache.ServerList)
	err = selector.SetServers(servers...)

	if err == nil {

		c.client = memcache.NewFromSelector(selector)
		c.client.Timeout = 200 * time.Millisecond // Default timeout: 200 ms.

		if defaultExpiration != nil {
			c.defaultExpiration = defaultExpiration[0]
		} else {
			c.defaultExpiration = 60 * 15 // Default expiration time: 15 minutes.
		}

		// Test the connection...
		if _, testErr := c.client.Get("GOLANG_TEST"); testErr != nil && testErr.Error() != "memcache: cache miss" {
			err = errors.New("Can't connect to the servers")
		}
	}

	return
}
コード例 #3
0
func (self *CacheMemcache) Connect() {
	ss := new(memcache.ServerList)
	ss.SetServers(self.Servers...)
	self.Client = memcache.NewFromSelector(ss)
}