Пример #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
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>
	// 400 Bad Request: Cannot increment or decrement non-numeric value
	// 400 Bad Request: Cannot increment or decrement non-numeric value
}
Пример #4
0
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)
		})
	})
}
Пример #5
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
}