Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is most optimal way to store an object in redis?

I have a class, which is modeled like this, all member variables are comprising of strings and integers.

> class XYZ extends CFormModel
    {
    //Values required for rendering the Dashboard
    public $username;
    public $analysis_type;
    public $trace_selection;
    public $filter_phantoms;
    public $trace_oui_map;
    public $frame_min;
    public $frame_max;
    public $time_end;
    public $frame_range;
    public $time_range;
    //Other Values
    private $RETURNURL;
    private $PARAMS;
    private $connection;        // connection to db client
    private $database;          // handle to database
    private $col_trace_info;    // handle to trace_info collection
    private $col_csv;           // handle to csv collection
    ...
    ...

I want to store this object of this class in redis cache for performance improvements. Solutions i have stumbled upon to is to use hashmaps..

Yii::app()->cache()->executeCommand("HSET", array("KEY"=>$hashMap, "FIELD"=>$key, "VALUE"=>$object));

My question is, is there a better way to store is object in memory, by using any other data structure or serialize it before storing, or something like that??

like image 416
Himanshu97 Avatar asked Dec 26 '22 09:12

Himanshu97


1 Answers

Hash Sets are not needed (nor preferred), if you need the complete object or record at retrieval in most of your scenario's. A Hash Set is like a mini-redis database inside Redis. Each key has overhead, and each member of a Hash Set has overhead.

I recommend this approach:

  1. Serialize your data as messagepack.
  2. Use a Hash Set, but put a complete record in each member of that Hash Set. Only reason for using a Hash Set: This helps keeping things organized. Simple get/set would work as well.
  3. If you want an numeric index 'almost' for free, use a Sorted Set instead of a Hash Set. You can use the score as an index. 2.8.9+: Raw alphabetical index also supported: Put everything under score 0 , prefix your msgpack data with an alphabetical identifier/searchstring (plus a delimiter, \t is normally a good one) and use the new function ZRANGEBYLEX.
  4. Pipeline your data
  5. When doing bulk transfers, wrap the data again in a messagepack container, 1000 records a piece (indication), and let a server-side Lua script do the work for you.

See also here and here

Hope this helps, TW

like image 103
Tw Bert Avatar answered Jan 12 '23 09:01

Tw Bert