Duplicate cached data
Create a second key containing the same cached value without changing the original.
Redis · Module
Copies the value of one Redis key to another key. It can target a different logical database and optionally replace an existing destination.
COPY duplicates the value held by one key under another key. It normally creates the destination in the connection's current logical database, but DB selects another database and REPLACE permits overwriting an existing destination.
COPY source destination [DB destination-db] [REPLACE]| Name | Type | Description |
|---|---|---|
source | key | Key whose value is copied. |
destination | key | Key to create with the copied value. |
destination-db | integer | Optional logical database index for the destination. |
REPLACE | flag | Optional flag that removes an existing destination before copying. |
Integer reply: 1 if source was copied; 0 if source was not copied.
SET dolly "sheep"
COPY dolly clone
GET cloneCreates a source string key, copies its value to a new key, then reads the copied value.
Create a second key containing the same cached value without changing the original.
Populate a destination key in a selected logical database.
Refresh a destination key by replacing its prior value.
COPY duplicates the value associated with source under destination. It returns 1 when the copy succeeds and 0 when the source was not copied, including when the destination already exists without REPLACE.
Follow-up: How can an existing destination be overwritten? Use the REPLACE option, which removes the destination key before copying the value.
By default, the destination is created in the logical database selected by the connection. The DB destination-db option directs the destination to a specified logical database index.
Follow-up: What deployment constraint applies to DB selection in clustered environments? The source and destination keys must be in the same hash slot for clustered or Active-Active databases.
COPY preserves the source value while creating a separate destination key. Its worst-case complexity is O(N) for collections, where N is the number of nested items, and O(1) for strings.
Follow-up: Why does data type matter for performance? Collection values require traversal proportional to their nested contents, while strings have constant-time copying.
DBREPLACESETGETDEL