Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for SET with NX and GET as per redis v7 #1339

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update set.js
support NX and GET as per redis v7
  • Loading branch information
akadrac authored Dec 18, 2023
commit d65cd3d6e183f84b0a51ccc1a47f16c4866731bf
12 changes: 10 additions & 2 deletions src/commands/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,24 @@ function createGroupedArray(arr, groupSize) {
export function set(key, value, ...options) {
const nx = options.indexOf('NX') !== -1
const xx = options.indexOf('XX') !== -1
const get = options.indexOf('GET') !== -1

const filteredOptions = options.filter(
option => option !== 'NX' && option !== 'XX'
)

if (nx && xx) throw new Error('ERR syntax error')
if (nx && this.data.has(key)) return null
if (nx && this.data.has(key)) {
if (get)
return this.data.get(key)
else
return null
}

if (xx && !this.data.has(key)) return null

let result = 'OK'
if (options.indexOf('GET') !== -1) {
if (get) {
result = this.data.has(key) ? this.data.get(key) : null
}

Expand Down