本文整理汇总了Golang中github.com/bradfitz/gomemcache/memcache.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Initialize
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
}
示例2: TestApp
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)
}
}
示例3: main
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)
}
示例4: TestAppIdleTimeout
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")
}
}
}
示例5: getData
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
}
示例6: dials
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
}
示例7: updateStateGroup
// 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)
}
示例8: main
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)
}
}
示例9: BenchmarkAdd
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()
}
示例10: initializeCache
func initializeCache() {
var err error
cacher = memcache.New(config.MCHost)
if err != nil {
logrus.Fatal("cache:", err)
}
}
示例11: main
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)
}
}
}
示例12: New
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}
}
示例13: setupServices
// 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)
}
}
}
示例14: makeHandler
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)
}
}
示例15: worker
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)
}
}
}
}