Type String overview:

  • String values are the simplest type of data in Redis.
  • Name “String” does not necessary means text value.
  • Redis “Strings” a “bit save” and therefore can store:
    • text values
    • numeric value – integer, real….
    • picture
    • data structure from different programming language.
  • Max. size is 512 MB.

String commands overview:

  • common value manipulation: SET, GET, GETRANGE, APPEND
  • integer numeric value manipulation: INCR, DECR, INCRBY, DECRBY
  • real/float numeric value manipulation: INCRBYFLOAT

SET:

  • basic and most versatile command for setting string value for the key
  • there are specialized variants: SETNX, SETEX, PSETEX – basically SET command with options bellow – can be removed in the future from redis!
  • syntax:
    • SET key-name “string-value”
    • SET key-name numeric-value
    • SET key-name some-bit/byte-dependant-structure
  • response:
    • OK
  • options – key exists/ does not exist:
    • NX – set value only if key DOES NOT exist
      • example:
        • set name “joe” nx
      • response:
        • if key “name” exists: (nil)
        • if key “name” does not exist and now is created: OK
    • XX – set value only if key DOES exist
      • example:
        • set name “joe” xx
      • response:
        • if key “name” exists and therefore is now updated: OK
        • if key does not exist: (nil)
  • options – expiration time – key is being held in memory only for specified time and after this time is deleted:
    • EX seconds
    • PX milliseconds
    • example:
      • set name “joe” ex 60
      • this key will exist only 1 minute – we can check it with command “keys *”

GET:

  • retrieves string value from the key
  • syntax:
    • GET key-name
  • response:
    • value if key exists
    • (nil) if key does not exist
    • (error) if key value is different type than string

GETRANGE:

  • returns “substring” of the value
  • first position is 0
  • syntax:
    • getrange key-name start end
  • example:
    • getrange name 0 2
      • returns first 3 characters
    • getrange name -3 -1
      • returns last 3 characters

INCR, DECR:

  • if key contains integer value you can increase / decrease it by 1
    • INCR key-name
    • DECR key-name

INCRBY, DECRBY:

  • increases / decreases integer value by given value:
    • INCRBY key-name 10
    • DECRBY key-name 10
  • attention – value lower than 0 performs oposite operation:
    • INCRBY key-name -10 = DECRBY key-name 10
    • DECRBY key-name -10 = INCRBY key-name 10

INCRBYFLOAT:

  • use it if key contains real/float numeric value
  • value bigger than 0 increases number in key
  • value lower than 0 decreases