コード例 #1
0
func Example4RetrievingData() {
	c := cache.New("test_cache")

	value, err := c.Get("number_item")
	fmt.Printf("%#v (%#v)\n", value, err)

	value, err = c.Get("string_item")
	fmt.Printf("%#v (%#v)\n", value, err)

	// JSON is returned as strings
	value, err = c.Get("complex_item")
	fmt.Printf("%#v (%#v)\n", value, err)

	// You can use the JSON codec to deserialize it.
	obj := struct {
		Args []string
		Test string
	}{}
	err = cache.JSON.Get(c, "complex_item", &obj)
	fmt.Printf("%#v (%#v)\n", obj, err)
	// Output:
	// 42 (<nil>)
	// "Hello, IronCache" (<nil>)
	// "{\"args\":[\"apples\",\"oranges\"],\"test\":\"this is a dict\"}" (<nil>)
	// struct { Args []string; Test string }{Args:[]string{"apples", "oranges"}, Test:"this is a dict"} (<nil>)
}
コード例 #2
0
func Example5DeletingData() {
	c := cache.New("test_cache")

	// Immediately delete an item
	c.Delete("string_item")

	p(c.Get("string_item"))
	// Output:
	// <nil> 404 Not Found: The resource, project, or endpoint being requested doesn't exist.
}
コード例 #3
0
ファイル: main.go プロジェクト: rdallman/slackbots
func main() {
	c = cache.New("uptime_bot")

	b, err := ioutil.ReadFile("config.json")

	if err != nil {
		fmt.Println(err)
		return
	}

	cfg := &Config{}
	err = json.Unmarshal(b, cfg)
	if err != nil {
		fmt.Println(err)
		return
	}

	client = &PingdomClient{
		Username: cfg.Username,
		Password: cfg.Password,
		ApiKey:   cfg.ApiKey,
	}

	slackClient = &slackC{
		url: cfg.WebhookUrl,
	}
	since := time.Now()
	mqUptimes := client.getUptimes(cfg.MqIds, since)
	workerUptimes := client.getUptimes(cfg.WorkerIds, since)
	otherUptimes := client.getUptimes(cfg.OtherIds, since)

	var attachments []Attachment
	attachments = append(attachments, buildReportsAttachment("IronMQ", mqUptimes))
	attachments = append(attachments, buildReportsAttachment("IronWorker", workerUptimes))
	attachments = append(attachments, buildReportsAttachment("Other", otherUptimes))

	var reports UptimeReports
	reports = append(reports, mqUptimes...)
	reports = append(reports, workerUptimes...)
	reports = append(reports, otherUptimes...)
	sort.Sort(reports)

	if len(reports) > 2 {
		if reports[0].DayAgo.uptimePercentage() < 1 {
			attachments = append(attachments, buildReportAttachment(reports[0]))
			attachments = append(attachments, buildReportAttachment(reports[1]))
		}
	}
	// fmt.Println("", attachments)
	slackClient.post("", attachments)
}
コード例 #4
0
func Example3Decrementing() {
	c := cache.New("test_cache")

	p(c.Increment("number_item", -10))
	p(c.Get("number_item"))

	p(c.Increment("string_item", -10))

	p(c.Increment("complex_item", -10))

	// Output:
	// <nil>
	// 42 <nil>
	// Cannot increment or decrement non-numeric value
	// Cannot increment or decrement non-numeric value
}
コード例 #5
0
ファイル: cache_test.go プロジェクト: wubin1989/iron_go
func init() {
	defer PrintSpecReport()

	Describe("IronCache", func() {
		c := cache.New("cachename")

		It("Lists all caches", func() {
			_, err := c.ListCaches(0, 100) // can't check the caches value just yet.
			Expect(err, ToBeNil)
		})

		It("Puts a value into the cache", func() {
			err := c.Put("keyname", &cache.Item{
				Value:      "value",
				Expiration: 2 * time.Second,
			})
			Expect(err, ToBeNil)
		})

		It("Gets a value from the cache", func() {
			value, err := c.Get("keyname")
			Expect(err, ToBeNil)
			Expect(value, ToEqual, "value")
		})

		It("Gets meta-information about an item", func() {
			err := c.Put("forever", &cache.Item{Value: "and ever", Expiration: 0})
			Expect(err, ToBeNil)
			value, err := c.GetMeta("forever")
			Expect(err, ToBeNil)
			Expect(value["key"], ToEqual, "forever")
			Expect(value["value"], ToEqual, "and ever")
			Expect(value["cache"], ToEqual, "cachename")
			Expect(value["expires"], ToEqual, "9999-01-01T00:00:00Z")
			Expect(value["flags"], ToEqual, 0.0)
		})

		It("Sets numeric items", func() {
			err := c.Set("number", 42)
			Expect(err, ToBeNil)
			value, err := c.Get("number")
			Expect(err, ToBeNil)
			Expect(value.(float64), ToEqual, 42.0)
		})
	})
}
コード例 #6
0
func Example1StoringData() {
	// For configuration info, see http://dev.iron.io/articles/configuration
	c := cache.New("test_cache")

	// Numbers will get stored as numbers
	c.Set("number_item", 42)

	// Strings get stored as strings
	c.Set("string_item", "Hello, IronCache")

	// Objects and dicts get JSON-encoded and stored as strings
	c.Set("complex_item", map[string]interface{}{
		"test": "this is a dict",
		"args": []string{"apples", "oranges"},
	})

	p("all stored")
	// Output:
	// all stored
}