+/* Save a key-value pair, with expire time, type, key, value.
+ * On error -1 is returned.
+ * On success if the key was actaully saved 1 is returned, otherwise 0
+ * is returned (the key was already expired). */
+int rdbSaveKeyValuePair(rio *rdb, robj *key, robj *val,
+ time_t expiretime, time_t now)
+{
+ int vtype;
+
+ /* Save the expire time */
+ if (expiretime != -1) {
+ /* If this key is already expired skip it */
+ if (expiretime < now) return 0;
+ if (rdbSaveType(rdb,REDIS_EXPIRETIME) == -1) return -1;
+ if (rdbSaveTime(rdb,expiretime) == -1) return -1;
+ }
+ /* Fix the object type if needed, to support saving zipmaps, ziplists,
+ * and intsets, directly as blobs of bytes: they are already serialized. */
+ vtype = val->type;
+ if (vtype == REDIS_HASH && val->encoding == REDIS_ENCODING_ZIPMAP)
+ vtype = REDIS_HASH_ZIPMAP;
+ else if (vtype == REDIS_LIST && val->encoding == REDIS_ENCODING_ZIPLIST)
+ vtype = REDIS_LIST_ZIPLIST;
+ else if (vtype == REDIS_SET && val->encoding == REDIS_ENCODING_INTSET)
+ vtype = REDIS_SET_INTSET;
+ else if (vtype == REDIS_ZSET && val->encoding == REDIS_ENCODING_ZIPLIST)
+ vtype = REDIS_ZSET_ZIPLIST;
+ /* Save type, key, value */
+ if (rdbSaveType(rdb,vtype) == -1) return -1;
+ if (rdbSaveStringObject(rdb,key) == -1) return -1;
+ if (rdbSaveObject(rdb,val) == -1) return -1;
+ return 1;