@@ -129,6 +129,80 @@ func Set[T any](c *Client, key string, item T, opts ...Option) error {
129129 })
130130}
131131
132+ // Replace will store the item using the given key, but only if the key
133+ // already exists. New items are at the top of the LRU.
134+ //
135+ // Uses Client c to connect to a memcached instance, and automatically handles
136+ // connection pooling and reuse.
137+ //
138+ // One or more Option(s) may be applied to configure things such as the
139+ // value expiration TTL or its associated flags.
140+ func Replace [T any ](c * Client , key string , item T , opts ... Option ) error {
141+ if err := check (key ); err != nil {
142+ return err
143+ }
144+
145+ options := & Options {
146+ expiration : c .expiration ,
147+ flags : 0 ,
148+ }
149+
150+ for _ , opt := range opts {
151+ opt (options )
152+ }
153+
154+ return c .do (key , func (conn * iopool.Buffer ) error {
155+ encoding , encerr := encode (item )
156+ if encerr != nil {
157+ return encerr
158+ }
159+
160+ expiration , experr := c .seconds (options .expiration )
161+ if experr != nil {
162+ return experr
163+ }
164+
165+ // write the header components
166+ if _ , err := fmt .Fprintf (
167+ conn ,
168+ "replace %s %d %d %d\r \n " ,
169+ key , options .flags , expiration , len (encoding ),
170+ ); err != nil {
171+ return err
172+ }
173+
174+ // write the payload
175+ if _ , err := conn .Write (encoding ); err != nil {
176+ return err
177+ }
178+
179+ // write clrf
180+ if _ , err := io .WriteString (conn , "\r \n " ); err != nil {
181+ return err
182+ }
183+
184+ // flush the buffer
185+ if err := conn .Flush (); err != nil {
186+ return err
187+ }
188+
189+ // read response
190+ line , lerr := conn .ReadSlice ('\n' )
191+ if lerr != nil {
192+ return lerr
193+ }
194+
195+ switch string (line ) {
196+ case "STORED\r \n " :
197+ return nil
198+ case "NOT_STORED\r \n " :
199+ return ErrNotStored
200+ default :
201+ return fmt .Errorf ("memc: unexpected response to replace: %q" , string (line ))
202+ }
203+ })
204+ }
205+
132206// Add will store the item using the given key, but only if no item currently
133207// exists. New items are at the top of the LRU.
134208//
0 commit comments