@@ -32,6 +32,13 @@ type Client struct {
3232// Values are automatically marshalled to JSON or gob (depending on the configuration).
3333// The key must not be "" and the value must not be nil.
3434func (c Client ) Set (k string , v interface {}) error {
35+ ctx , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
36+ defer cancel ()
37+ return c .SetWithContext (ctx , k , v )
38+ }
39+
40+ // SetWithContext is exactly like Set function just with added context as first argument.
41+ func (c Client ) SetWithContext (ctx context.Context , k string , v interface {}) error {
3542 if err := util .CheckKeyAndValue (k , v ); err != nil {
3643 return err
3744 }
@@ -42,16 +49,14 @@ func (c Client) Set(k string, v interface{}) error {
4249 return err
4350 }
4451
45- tctx , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
46- defer cancel ()
4752 key := datastore.Key {
4853 Kind : kind ,
4954 Name : k ,
5055 }
5156 src := entity {
5257 V : data ,
5358 }
54- _ , err = c .c .Put (tctx , & key , & src )
59+ _ , err = c .c .Put (ctx , & key , & src )
5560
5661 return err
5762}
@@ -63,18 +68,23 @@ func (c Client) Set(k string, v interface{}) error {
6368// If no value is found it returns (false, nil).
6469// The key must not be "" and the pointer must not be nil.
6570func (c Client ) Get (k string , v interface {}) (found bool , err error ) {
71+ ctx , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
72+ defer cancel ()
73+ return c .GetWithContext (ctx , k , v )
74+ }
75+
76+ // GetWithContext is exactly like Get function just with added context as first argument.
77+ func (c Client ) GetWithContext (ctx context.Context , k string , v interface {}) (found bool , err error ) {
6678 if err := util .CheckKeyAndValue (k , v ); err != nil {
6779 return false , err
6880 }
6981
70- tctx , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
71- defer cancel ()
7282 key := datastore.Key {
7383 Kind : kind ,
7484 Name : k ,
7585 }
7686 dst := new (entity )
77- err = c .c .Get (tctx , & key , dst )
87+ err = c .c .Get (ctx , & key , dst )
7888 if err != nil {
7989 if err == datastore .ErrNoSuchEntity {
8090 return false , nil
@@ -90,17 +100,22 @@ func (c Client) Get(k string, v interface{}) (found bool, err error) {
90100// Deleting a non-existing key-value pair does NOT lead to an error.
91101// The key must not be "".
92102func (c Client ) Delete (k string ) error {
103+ ctx , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
104+ defer cancel ()
105+ return c .DeleteWithContext (ctx , k )
106+ }
107+
108+ // DeleteWithContext is exactly like Delete function just with added context as first argument.
109+ func (c Client ) DeleteWithContext (ctx context.Context , k string ) error {
93110 if err := util .CheckKey (k ); err != nil {
94111 return err
95112 }
96113
97- tctx , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
98- defer cancel ()
99114 key := datastore.Key {
100115 Kind : kind ,
101116 Name : k ,
102117 }
103- return c .c .Delete (tctx , & key )
118+ return c .c .Delete (ctx , & key )
104119}
105120
106121// Close closes the client.
0 commit comments