AkashBabu / redis-json

A wrapper library to store JSON Objects in redis-hashsets and retrieve it back as JSON objects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

incorrect handling of removed array indexes and object properties

fpizzo20 opened this issue · comments

I've found a strage behavior of the .set() method working with arrays.

The strage and not documented behavior is that if an array becomes shorter than the last stored in redis, the last few keys are note deleted.

Test case:

const client = this.redisService.getClient();
const usersMap = new JSONCache<number[]>(client);
const arr1 = [1,2,3];
usersMap.set('test', arr1);

let arr2 = usersMap.get('test');

arr2  = arr2 .filter(el=> {
    return el % 2 == 1;
});

// arr2 = [ 1, 3 ]

usersMap.set('test', arr2);

const arr3 = usersMap.get('test');

// arr3 = [ 1, 3, 3 ]

This 'bug' happens because this library uses hmset redis command, that does only upsert but does not remove missing keys.

This library offerse the possibility to retrieve partial objects, so is not possibile to remove missing object props / array keys on set.

I think that a big disclaimer on this behavior should be added to the docs.

Would you accept a pull request that adds some new methods to allow a user to 'set and replace with delete missing props' objects in this cache in the stored objects are very little?

Thanks
Francesco

@fpizzo20 thanks for reporting this bug.
Yes you are most welcome to raise a PR, but will be accepted only with the corresponding test cases

Please upgrade your library to v6.0.0

Also please pay attention to migration guide