func (self *FSMemcache) Initialize() error {
	self.conn = memcache.New(self.ServerList...)
	if self.conn == nil {
		return errors.New("Unable to initialize memcache driver")
	}
	return nil
}
Beispiel #2
0
func TestApp(t *testing.T) {
	app := newTestAppAndListenTCP(t)
	mc := memcache.New(app.Listener.Addr().String())

	item, err := mc.Get("hoge")
	if err != nil {
		t.Fatal(err)
	}

	t.Logf("key = %s", item.Key)
	t.Logf("flags = %d", item.Flags)
	t.Logf("id = %s", item.Value)

	if k := item.Key; k != "hoge" {
		t.Errorf("Unexpected key: %s", k)
	}

	if f := item.Flags; f != 0 {
		t.Errorf("Unexpected flags: %d", f)
	}

	if _, err := strconv.ParseInt(string(item.Value), 10, 64); err != nil {
		t.Errorf("Invalid id: %s", err)
	}
}
Beispiel #3
0
func main() {

	fmt.Println("Iniciando Webserver....")
	hostname := os.Getenv("HOSTNAME")
	if hostname != "" {
		fmt.Println("Running from Container")
		memcachedServer = "memcached1"
		rabbitServer = "some-rabbit"
	} else {
		fmt.Println("Running from Localhost")
	}

	//Esta IP es la IP del container, también se puede poner la de la máquina virtual y funciona
	//(que es más práctico porque es siempre la misma para todos los containers), la otra es poner el nomber del link

	//Create the memcached client with the container name linked
	mc = memcache.New(memcachedServer + ":11211")

	//Create the Rabbit connection
	initRabbitMQ()

	//Creathe the webserver
	http.HandleFunc("/", handler)
	http.ListenAndServe(":8080", nil)
}
Beispiel #4
0
func TestAppIdleTimeout(t *testing.T) {
	app := newTestAppAndListenTCP(t)
	app.SetIdleTimeout(1)

	mc := memcache.New(app.Listener.Addr().String())

	t.Log("Before timeout")
	{
		item, err := mc.Get("hoge")
		if err != nil {
			t.Fatal(err)
		}

		if _, err := strconv.ParseInt(string(item.Value), 10, 64); err != nil {
			t.Errorf("Invalid id: %s", err)
		}
	}

	time.Sleep(2 * time.Second)

	t.Log("After timeout")
	{
		_, err := mc.Get("hoge")
		if err == nil {
			t.Fatal("Connection must be disconnected")
		}
	}
}
Beispiel #5
0
func getData(url string) (rv []byte, err error) {
	mc := memcache.New(*mcdServer)

	itm, err := mc.Get(url)
	if err != nil {
		log.Printf("Fetching %v", url)
		resp, err := http.Get(url)
		if err != nil {
			return rv, err
		}
		defer resp.Body.Close()
		data, err := ioutil.ReadAll(resp.Body)

		itm = &memcache.Item{
			Key:        url,
			Value:      data,
			Expiration: (86400 * 15),
		}
		err = mc.Set(itm)
		if err != nil {
			log.Printf("Error caching %v: %v", url, err)
		}
	}
	return itm.Value, nil
}
Beispiel #6
0
func (c *connMc) dials() error {
	resp, err := c.etcdClient.Get(c.etcdKey+"/servers", true, false)
	if err != nil {
		log.Printf("etcd get error: %s", err)
		return err
	}
	if len(resp.Node.Nodes) == 0 {
		log.Printf("no UQ server registered in etcd")
		return errors.New("no UQ server registered in etcd")
	}

	c.addrs = make([]string, 0)
	for i, node := range resp.Node.Nodes {
		parts := strings.Split(node.Key, "/")
		log.Printf("parts: %v", parts)

		addr := parts[len(parts)-1]
		log.Printf("server-%d : %s", i, addr)
		conn := memcache.New(addr)
		c.addrs = append(c.addrs, addr)
		c.conns[addr] = conn
	}

	log.Printf("mc conn succ: %v", len(c.conns))

	return nil
}
Beispiel #7
0
// Updates a task state group
func (memcacheBackend *MemcacheBackend) updateStateGroup(groupUUID string, taskState *TaskState) error {
	var taskStateGroup TaskStateGroup

	client := memcache.New(memcacheBackend.servers...)

	item, err := client.Get(groupUUID)
	if err != nil {
		taskStateGroup = TaskStateGroup{
			GroupUUID: groupUUID,
			States:    make(map[string]TaskState),
		}
	} else {
		if err := json.Unmarshal(item.Value, &taskStateGroup); err != nil {
			log.Printf("Failed to unmarshal task state group: %v", string(item.Value))
			log.Print(err)
			return err
		}
	}

	taskStateGroup.States[taskState.TaskUUID] = *taskState

	encoded, err := json.Marshal(taskStateGroup)
	if err != nil {
		return fmt.Errorf("JSON Encode Message: %v", err)
	}

	if err := client.Set(&memcache.Item{
		Key:   groupUUID,
		Value: encoded,
	}); err != nil {
		return err
	}

	return memcacheBackend.setExpirationTime(groupUUID)
}
Beispiel #8
0
func main() {
	// Connect to our memcache instance
	mc := memcache.New("127.0.0.1:11111")
	err := mc.Set(&memcache.Item{Key: "key_one", Value: []byte("michael")})
	if err != nil {
		log.Fatal(err)
	}
	err = mc.Set(&memcache.Item{Key: "key_two", Value: []byte("programming")})
	if err != nil {
		log.Fatal(err)
	}

	val, err := mc.Get("key_one")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("key_one: %s\n", val.Value)

	it, err := mc.GetMulti([]string{"key_one", "key_two"})
	if err != nil {
		log.Fatal(err)
	}
	for k, v := range it {
		fmt.Printf("%s -> %s\n", k, v.Value)
	}
}
func BenchmarkAdd(b *testing.B) {
	b.StopTimer()

	keyPrefix := "set_key"
	var mcs [nroutine]*memcache.Client
	for i := 0; i < nroutine; i++ {
		mcs[i] = memcache.New("127.0.0.1:11211")

		// pre get because the lazy connecting strategy
		mcs[i].Get(keyPrefix)
	}

	b.StartTimer()

	var wg sync.WaitGroup
	nloop := b.N / nroutine * moretimes
	for i := 0; i < nroutine; i++ {
		wg.Add(1)
		go func(idx int) {
			defer wg.Done()

			for j := 0; j < nloop; j++ {
				key := fmt.Sprintf("%s_%d_%d", keyPrefix, i, j)
				mcs[idx].Get(key)
			}
		}(i)
	}
	wg.Wait()
}
Beispiel #10
0
func initializeCache() {
	var err error
	cacher = memcache.New(config.MCHost)
	if err != nil {
		logrus.Fatal("cache:", err)
	}
}
func main() {
	var (
		host    = flag.String("host", "memcached", "memcached host or ip address")
		command = flag.String("command", "get", "get set, state")
		key     = flag.String("key", "key", "key")
		value   = flag.String("value", "value", "value")
	)
	flag.Parse()

	mc := memcache.New(*host + ":11211")
	mc.Timeout = time.Second

	switch *command {
	case "get":
		item, err := mc.Get(*key)
		if err == nil {
			fmt.Printf("got %s -> %s\n", *key, string(item.Value))
		} else {
			fmt.Printf("error %v\n", err)
		}
	case "set":
		item := memcache.Item{Key: *key, Value: []byte(*value)}
		err := mc.Set(&item)
		if err == nil {
			fmt.Printf("set %s -> %s\n", *key, *value)
		} else {
			fmt.Printf("error %v\n", err)
		}
	}
}
Beispiel #12
0
func New(opts util.JsMap, log *util.HekaLogger) *Storage {

	config = opts
	var ok bool

	if _, ok = config["memcache.server"]; !ok {
		config["memcache.server"] = "127.0.0.1:11211"
	}

	if _, ok = config["db.timeout_live"]; !ok {
		config["db.timeout_live"] = "259200"
	}

	if _, ok = config["db.timeout_reg"]; !ok {
		config["db.timeout_reg"] = "10800"
	}

	if _, ok = config["db.timeout_del"]; !ok {
		config["db.timeout_del"] = "86400"
	}
	if _, ok = config["shard.defaultHost"]; !ok {
		config["shard.defaultHost"] = "localhost"
	}
	if _, ok = config["shard.currentHost"]; !ok {
		config["shard.currentHost"] = config["shard.defaultHost"]
	}
	if _, ok = config["shard.prefix"]; !ok {
		config["shard.prefix"] = "_h-"
	}

	log.Info("storage", "Creating new memcache handler", nil)
	return &Storage{mc: memcache.New(config["memcache.server"].(string)),
		config: config,
		log:    log}
}
Beispiel #13
0
// setup connections to other services running on this host
func setupServices() {
	var error error
	services.Statsd, error = g2s.Dial("udp", "localhost:8125")
	if error != nil {
		log.Fatal("could not set up statsd client.")
	}
	services.Memcached = memcache.New("localhost:11211")

	// setup push service
	usingSandbox := "true" // true or false
	uniqushResponse, uniqushError := http.PostForm("http://localhost:9898/addpsp", url.Values{
		"pushservicetype": {"apns"},
		"service":         {"newspeak"},
		"cert":            {"/etc/newspeak/apns-certs/cert.pem"},
		"key":             {"/etc/newspeak/apns-certs/priv-noenc.pem"},
		"sandbox":         {usingSandbox},
	})
	if uniqushError != nil {
		log.Fatal("could not add push service provider for apple push notifications: " + string(uniqushError.Error()))
	} else {
		uniqushResponseBodyBytes, uniqushError := ioutil.ReadAll(uniqushResponse.Body)
		uniqushResponseBody := string(uniqushResponseBodyBytes)
		uniqushResponse.Body.Close()
		if uniqushError != nil {
			log.Fatal("could not read response when adding push service provider for apple push notifications: " + string(uniqushError.Error()))
		} else if uniqushResponseBody[0:30] != "[AddPushServiceProvider][Info]" {
			log.Fatal("invalid response when adding push service provider for apple push notifications: " + uniqushResponseBody)
		} else {
			fmt.Println("added push service provider for apple push notifications. usingSandbox:" + usingSandbox + ", uniqush response:" + uniqushResponseBody)
		}
	}
}
Beispiel #14
0
func makeHandler() web.HandlerFunc {
	db, err := sql.Open("sqlite3", databaseFile)
	if err != nil {
		panic(err)
	}
	mc := memcache.New(memcacheServer)
	return func(req *web.RequestHandler) {
		req.SetHeader("Access-Control-Allow-Origin", "*")
		k := strings.Split(req.HTTP.RemoteAddr, ":")[0]
		// Check quota
		el, err := mc.Get(k)
		if err == memcache.ErrCacheMiss {
			err = mc.Set(&memcache.Item{
				Key: k, Value: []byte("1"),
				Expiration: expirySeconds})
		}
		if err != nil {
			req.HTTPError(503, "memcache: %s", err.Error())
			return
		}
		if el != nil {
			count, _ := strconv.Atoi(string(el.Value))
			if count < maxRequestsPerIP {
				mc.Increment(k, 1)
			} else {
				req.HTTPError(403, "")
				return
			}
		}
		Lookup(req, db)
	}
}
Beispiel #15
0
func worker(queue chan []work, opts options, wg *sync.WaitGroup) {
	defer wg.Done()
	mc := memcache.New(opts.hostport)
	for batch := range queue {
		for _, work := range batch {
			ok := false
			var i uint

			for i = 1; i <= opts.retry; i++ {
				err := mc.Set(&memcache.Item{Key: work.id, Value: work.blob})
				if err != nil {
					pause := 2 << i * backoff
					if opts.verbose {
						log.Printf("retry %d for %s in %s (%s)", i, work.id, pause, err)
					}
					time.Sleep(pause)
				} else {
					ok = true
					break
				}
			}
			if !ok {
				log.Fatal(errSetFailed)
			}
		}
	}
}
Beispiel #16
0
func main() {
	appId := flag.String("appid", "", "weixin appid")
	appSecret := flag.String("appsecret", "", "weixin appsecret")
	accessTokenKeyFlag := flag.String("a", "access_token", "access token key")
	jssdkTicketKeyFlag := flag.String("j", "jssdk_ticket", "jssdk ticket key")
	memcacheAddr := flag.String("mem_addr", "127.0.0.1:11211", "memcache addr")
	_ = flag.String("redis_addr", "127.0.0.1:6379", "redis addr")

	flag.Parse()

	accessTokenKey = *accessTokenKeyFlag
	jssdkTicketKey = *jssdkTicketKeyFlag

	log.Printf("appId=%s\n", *appId)
	log.Printf("appSecret=%s\n", *appSecret)
	log.Printf("accessTokenKey=%s\n", accessTokenKey)
	log.Printf("jssdkTicketKey=%s\n", jssdkTicketKey)
	log.Printf("memcacheAddr=%s\n", *memcacheAddr)

	accessTokenCH := make(chan string)
	wx = weixin.New(*appId, *appSecret)
	mc = memcache.New(*memcacheAddr)

	go cacheAccessToken(accessTokenCH)
	go cacheJSSDKTicket(accessTokenCH)

	for {
		time.Sleep(time.Second)
	}
}
Beispiel #17
0
func newRateLimiter(c *Config) (*httprl.RateLimiter, error) {
	var backend httprl.Backend
	switch c.RateLimitBackend {
	case "map":
		m := httprl.NewMap(1)
		m.Start()
		backend = m
	case "redis":
		addrs := strings.Split(c.RedisAddr, ",")
		rc, err := redis.NewClient(addrs...)
		if err != nil {
			return nil, err
		}
		rc.SetTimeout(c.RedisTimeout)
		backend = redisrl.New(rc)
	case "memcache":
		addrs := strings.Split(c.MemcacheAddr, ",")
		mc := memcache.New(addrs...)
		mc.Timeout = c.MemcacheTimeout
		backend = memcacherl.New(mc)
	default:
		return nil, fmt.Errorf("unsupported backend: %q" + c.RateLimitBackend)
	}
	rl := &httprl.RateLimiter{
		Backend:  backend,
		Limit:    c.RateLimitLimit,
		Interval: int32(c.RateLimitInterval.Seconds()),
		ErrorLog: c.errorLogger(),
		//Policy:   httprl.AllowPolicy,
	}
	return rl, nil
}
Beispiel #18
0
func NewApiHandler(server_config *Config) *ApiHandler {
	h := new(ApiHandler)
	h.SetAccessLog(server_config)
	h.Servlets = make(map[string]Servlet)
	h.Memcached = memcache.New(server_config.Memcache.Host)
	return h
}
Beispiel #19
0
func stressMc(idx, n int) {
	// mc := memcache.New("192.168.1.227:11213", "192.168.1.226:11213")
	mc := memcache.New("192.168.1.226:11212")
	for {
		nsf := 0
		ts1 := time.Now().Unix()
		for i := 0; i < n; i += 1 {
			// fmt.Println("set", i, "goroutine", idx)
			key := "foo-" + strconv.Itoa(idx) + "-" + strconv.Itoa(i)
			err := mc.Set(&memcache.Item{Key: key, Value: []byte("my valuejjjjjjjjjjjjjjjjjjjjjjjjjjjjjllllllllllllllllllllllllllllllllllllaaaaaaaaaaaaaaaaaaaaaaaaf" + strconv.Itoa(i))})
			if err != nil {
				// fmt.Println("err happend setting: ", key);
				nsf += 1
			}
		}
		ts2 := time.Now().Unix()
		fmt.Println("index", idx, "set "+strconv.Itoa(n)+" keys, time:", ts2-ts1, "fail:", nsf)

		ngf := 0
		for i := 0; i < n; i += 1 {
			// fmt.Println("get", i, "goroutine", idx)
			key := "foo-" + strconv.Itoa(idx) + "-" + strconv.Itoa(i)
			_, err := mc.Get(key)
			if err != nil {
				// fmt.Println("err happend getting: ", key);
				ngf += 1
			}
		}
		ts3 := time.Now().Unix()
		fmt.Println("index", idx, "get "+strconv.Itoa(n)+" keys, time:", ts3-ts2, "fail:", ngf)
		// fmt.Println("index", idx, "set fail: ", nsf,  "get fail: ", ngf)
		time.Sleep(1 * time.Second)
	}
}
Beispiel #20
0
// MustClient will use the cache cluster ID to describe
// the cache cluster and instantiate a memcache.Client
// with the cache nodes returned from AWS.
func (e *ElastiCache) MustClient() *memcache.Client {
	var creds *credentials.Credentials
	if e.AccessKey != "" {
		creds = credentials.NewStaticCredentials(e.AccessKey, e.SecretKey, "")
	} else {
		creds = credentials.NewEnvCredentials()
	}

	ecclient := elasticache.New(session.New(&aws.Config{
		Credentials: creds,
		Region:      &e.Region,
	}))

	resp, err := ecclient.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{
		CacheClusterId:    &e.ClusterID,
		ShowCacheNodeInfo: aws.Bool(true),
	})
	if err != nil {
		log.Fatalf("unable to describe cache cluster: %s", err)
	}

	var nodes []string
	for _, cluster := range resp.CacheClusters {
		for _, cnode := range cluster.CacheNodes {
			addr := fmt.Sprintf("%s:%d", *cnode.Endpoint.Address, *cnode.Endpoint.Port)
			nodes = append(nodes, addr)
		}
	}

	return memcache.New(nodes...)
}
Beispiel #21
0
//CreateCStore will create a new CStore
func CreateMCStore(expiresTime time.Duration, servers []string) *MCStore {
	store := new(MCStore)

	store.mc = memcache.New(servers...)
	store.mc.Timeout = expiresTime

	return store
}
Beispiel #22
0
func init() {
	servers, err := getServerList()
	if err != nil {
		panic(err)
	}

	client = memcache.New(servers...)
}
Beispiel #23
0
func getGomemcacheClient(nServers int) *memcache.Client {
	servers := make([]string, nServers)
	for i := 0; i < nServers; i++ {
		servers[i] = fmt.Sprintf("localhost:%d", 21211+i)
	}

	return memcache.New(servers...)
}
Beispiel #24
0
func initMemcacheConn() {
	var (
		host = config.GetConf().String("memcache::host")
		port = config.GetConf().String("memcache::port")
	)

	mClient = memcache.New(host + ":" + port)
}
func NewCache(connstr string, handler http.Handler, limit Bytes, limiter LimiterFactory) http.Handler {
	return &Cache{
		mc:      memcache.New(connstr),
		handler: handler,
		Limit:   limit,
		limiter: limiter,
	}
}
Beispiel #26
0
func NewMemcache(servers []string, cacheKeyPrefix string) *Memcache {
	m := new(Memcache)
	m.mc = memcache.New(servers...)

	m.keyprefix = cacheKeyPrefix

	return m
}
Beispiel #27
0
func NewKV(addrs []string) kv.KV {
	if len(addrs) == 0 {
		addrs = []string{"127.0.0.1:11211"}
	}
	return &mkv{
		Client: mc.New(addrs...),
	}
}
Beispiel #28
0
func New(server ...string) *Provider {
	if len(server) == 0 {
		panic("[memcacheProvider.New] len(server)==0")
	}
	return &Provider{
		Client: memcache.New(server...),
	}
}
func main() {
	mc := memcache.New("127.0.0.1:11211")

	gtk.Init(nil)

	window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
	window.SetTitle("memcachedclient")
	window.Connect("destroy", gtk.MainQuit)

	vbox := gtk.NewVBox(false, 1)

	swin := gtk.NewScrolledWindow(nil, nil)
	textview := gtk.NewTextView()
	textview.SetEditable(false)
	textview.SetCursorVisible(false)
	textview.ModifyFontEasy("monospace 12")
	swin.Add(textview)
	vbox.Add(swin)

	buffer := textview.GetBuffer()

	input := gtk.NewEntry()
	input.SetEditable(true)
	vbox.PackEnd(input, false, false, 0)

	tag := buffer.CreateTag("red", map[string]string{"foreground": "#FF0000"})
	input.Connect("activate", func() {
		var iter gtk.TextIter
		tokens := strings.SplitN(input.GetText(), " ", 3)
		if len(tokens) == 2 && strings.ToUpper(tokens[0]) == "GET" {
			if t, err := mc.Get(tokens[1]); err == nil {
				buffer.GetEndIter(&iter)
				buffer.Insert(&iter, string(t.Value)+"\n")
			} else {
				buffer.InsertWithTag(&iter, err.Error()+"\n", tag)
			}
			input.SetText("")
		} else if len(tokens) == 3 && strings.ToUpper(tokens[0]) == "SET" {
			if err := mc.Set(
				&memcache.Item{
					Key:   tokens[1],
					Value: []byte(tokens[2]),
				}); err == nil {
				buffer.GetEndIter(&iter)
				buffer.InsertWithTag(&iter, "OK\n", tag)
			} else {
				buffer.InsertWithTag(&iter, err.Error()+"\n", tag)
			}
			input.SetText("")
		}
	})
	input.GrabFocus()

	window.Add(vbox)
	window.SetSizeRequest(300, 500)
	window.ShowAll()
	gtk.Main()
}
Beispiel #30
0
func New(config *Config, sid string) *Memcache {
	m := &Memcache{
		config: config,
		sid:    sid,
		client: memcache.New(config.Host),
	}

	return m
}