Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Set | Tipi di Dati in Redis
Introduzione a Redis

bookSet

Una delle caratteristiche principali di un set è che non consente valori duplicati. Quando si aggiunge un nuovo elemento a un set, se l'elemento esiste già, non verrà aggiunto nuovamente.

I set sono comunemente utilizzati per memorizzare valori unici, come ID utente, indirizzi IP o qualsiasi altro dato in cui è necessario evitare duplicati.

Utilizzo pratico dei set in Redis

Comandi di base per lavorare con i Set

I set di Redis dispongono di diversi comandi che facilitano l'aggiunta, la rimozione e la verifica degli elementi

Aggiunta e rimozione di elementi

Il comando SADD aggiunge elementi a un set, ignorando i duplicati, mentre il comando SREM rimuove elementi da un set.

SADD users "user1" "user2"  # adds "user1" and "user2" to the users set
SREM users "user1"  # removes "user1" from the users set

Verifica di un elemento e recupero di tutti gli elementi

Per verificare se un determinato elemento è presente in un set, utilizzare il comando SISMEMBER, che restituisce 1 se l'elemento è presente e 0 se non lo è. Per ottenere tutti gli elementi del set, utilizzare il comando SMEMBERS.

SISMEMBER users "user2"  # checks if "user2" is in the users set (returns 1 or 0)
SMEMBERS users  # returns all elements of the users set

Ottenere informazioni sul set

Per ottenere il numero di elementi in un set, utilizzare il comando SCARD, che restituisce il numero di elementi presenti nel set.

SCARD users  # returns the number of elements in the users set

Operazioni con più Set

Quando si lavora con più set, è possibile utilizzare i comandi SDIFF, SINTER e SUNIONSDIFF restituisce gli elementi che sono presenti in un set ma non negli altri, SINTER individua gli elementi comuni tra tutti i set specificati, e SUNION restituisce l'unione di tutti gli elementi provenienti da più set.

SDIFF set1 set2  # returns elements that are in `set1` but not in `set2`
SINTER set1 set2  # returns common elements between `set1` and `set2`
SUNION set1 set2  # returns the union of elements from `set1` and `set2`

1. Cosa fa il comando SADD in Redis?

2. Quale comando dovresti usare per recuperare tutti gli elementi da un set?

question mark

Cosa fa il comando SADD in Redis?

Select the correct answer

question mark

Quale comando dovresti usare per recuperare tutti gli elementi da un set?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 4

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Awesome!

Completion rate improved to 3.33

bookSet

Scorri per mostrare il menu

Una delle caratteristiche principali di un set è che non consente valori duplicati. Quando si aggiunge un nuovo elemento a un set, se l'elemento esiste già, non verrà aggiunto nuovamente.

I set sono comunemente utilizzati per memorizzare valori unici, come ID utente, indirizzi IP o qualsiasi altro dato in cui è necessario evitare duplicati.

Utilizzo pratico dei set in Redis

Comandi di base per lavorare con i Set

I set di Redis dispongono di diversi comandi che facilitano l'aggiunta, la rimozione e la verifica degli elementi

Aggiunta e rimozione di elementi

Il comando SADD aggiunge elementi a un set, ignorando i duplicati, mentre il comando SREM rimuove elementi da un set.

SADD users "user1" "user2"  # adds "user1" and "user2" to the users set
SREM users "user1"  # removes "user1" from the users set

Verifica di un elemento e recupero di tutti gli elementi

Per verificare se un determinato elemento è presente in un set, utilizzare il comando SISMEMBER, che restituisce 1 se l'elemento è presente e 0 se non lo è. Per ottenere tutti gli elementi del set, utilizzare il comando SMEMBERS.

SISMEMBER users "user2"  # checks if "user2" is in the users set (returns 1 or 0)
SMEMBERS users  # returns all elements of the users set

Ottenere informazioni sul set

Per ottenere il numero di elementi in un set, utilizzare il comando SCARD, che restituisce il numero di elementi presenti nel set.

SCARD users  # returns the number of elements in the users set

Operazioni con più Set

Quando si lavora con più set, è possibile utilizzare i comandi SDIFF, SINTER e SUNIONSDIFF restituisce gli elementi che sono presenti in un set ma non negli altri, SINTER individua gli elementi comuni tra tutti i set specificati, e SUNION restituisce l'unione di tutti gli elementi provenienti da più set.

SDIFF set1 set2  # returns elements that are in `set1` but not in `set2`
SINTER set1 set2  # returns common elements between `set1` and `set2`
SUNION set1 set2  # returns the union of elements from `set1` and `set2`

1. Cosa fa il comando SADD in Redis?

2. Quale comando dovresti usare per recuperare tutti gli elementi da un set?

question mark

Cosa fa il comando SADD in Redis?

Select the correct answer

question mark

Quale comando dovresti usare per recuperare tutti gli elementi da un set?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 4
some-alt