Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 543 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List of Dicts in Redis

#1
How to keep a list of dicts in Redis against a key using Python-redis. The following is the data structure which I am aiming at:

'browsing_history' : {
'session_key_1' : [{'image': 'image-url', 'url' : 'url', 'title' : 'test_title', 'description' : 'test_description'}, {''image': 'image-url2', 'url' : 'url2', 'title' : 'test_title2', 'description' : 'test_description2'}],
'session_key_2' : [{'image': 'image-url', 'url' : 'url', 'title' : 'test_title', 'description' : 'test_description'}, {''image': 'image-url2', 'url' : 'url2', 'title' : 'test_title2', 'description' : 'test_description2'}],
}

Would like to add into the session lists as well as add new sessions and also retrive them. How can I do this using Python-redis?
Reply

#2
Serialize your dictionary `{'image': 'image-url', 'url' : 'url', 'title' : 'test_title', 'description' : 'test_description'}` with [pickle][1] or [json][2]. Use redis list to store them as strings. Use keys like `browsing_history:SESSION_KEY_1` to access those lists. If you need to get a list of all session keys you probably will need to maintain a set of strings for keys `browsing_history:*`.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#3
A solution not requiring serialization and not restricted by the limits on string sizes (but not necessarily more performant) is to store each dict in its own dedicated hash map:

```
# define root name for hashes used
# to store list elements - dicts
hash_root_name='test_hash'

# sample list of dicts
dicts_list=[test_dict1, test_dict2]

# store dicts from the list to consecutively
# named redis hashes, with list indices
# appended to hash root name
for i in range(len(dicts_list)):

redis_client.hmset(hash_root_name+str(i),
dicts_list[i])

```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through