目錄mysql
最近須要一張用戶信息表,由於數據量並不大,想先放在內存中,等需求變動了,再移到磁盤上,或者往mysql塞,那麼問題來了,怎麼用redis的數據類型設計一個關係數據庫呢。c++
redis只有key-value這種存儲結構,若是想利用它作成想其餘數據庫同樣具有 增刪改查
等功能只能再設計了,這裏分享一下個人設計方法,比較簡單,我不知道算不算好,只是網上找了好久沒找到一種通用的方法,若是有什麼好的方法,還想請教一下各位,十分感謝。redis
key值
: 域名:表名:主鍵
value值
:直接使用redis的Hash類型
如:sql
test:accounts_info:0 id 0 accounts ailumiyana_0 password 123456 nick_name sola_0 test:accounts_info:1 id 1 accounts ailumiyana_1 password 123456 nick_name sola_1 test:accounts_info:2 id 2 accounts ailumiyana_2 password 123456 nick_name sola_2 test:accounts_info:3 id 3 accounts ailumiyana_3 password 123456 nick_name sola_3
另添加一個set集存放表主鍵, 也即id.
key值
: ids:域名:表名
value值
: id
將已生成的用戶id同時添加進set集合中.
我這裏用了list演示,不設計類型的特殊方法的話,演示效果是同樣的。
數據庫
有了set表後咱們就可使用redis中sort的get方法,獲取全部記錄.
sort ids:test:accounts_info get test:accounts_info:*->accounts get test:accounts_info:*->nick_name
數據庫設計
直接使用string類型創建主鍵到id的索引,其實id就是主鍵,可是咱們通常不會用id去找記錄,更多的使用account帳號去找.
key值
: 域名:表名:列鍵名:列鍵值
這樣咱們直接用get 取得accounts的id 後再去hash中查找記錄就好了.
ui
最後能夠根據表的須要創建一些其餘索引,
方法同 2 ,使用類型不必定是set 哪一個方便用哪一個。
例如 我要統計最近登陸的10個用戶的信息, 那麼我直接用list 的 lrange limit 0 10 就能取到.
this
以上設計的c++實現,其中的redis的客戶端使用了cpp_redis庫。設計
例程中 :
一、我建立了一張 account_info的表 默認使用accounts 做爲主鍵3d
二、插入4個用戶信息.
三、查詢用戶ailu_1的記錄值.
class table// : public redis::er_table { public: //! ctor table(void); //! dtor ~table(void) = default; //! copy ctor table(const table&) = delete; //! assignment operator table& operator=(const table&) = delete; public: //! vector type to save table records. typedef std::vector<std::string> records_t; //! vector type to save table records entitys. typedef std::vector<std::string> entitys_t; public: //! create table, //! default primary key is the records_t vec[0], if primary_key is empty! void create(const std::string& table_name, const records_t& vec, const std::string& primary_key = ""); public: //! incr primary key id. std::string incr_id(); //! insert new entity to table, pelease orderly insert refer to records vector ! //! return false while entity exits. bool insert(const entitys_t& vec); //! get entitys by primary key value. entitys_t get_entitys_by_primary_key_value(const std::string& primary_key_value); private: //! get id by primary key value //! retrun "" while primary key inexitences. std::string get_id_by_primary_key_value(const std::string& primary_key_value); private: //! redis client cpp_redis::client m_redis_client; //! records_t m_records; //! records count. std::size_t m_records_count; //! ids set key std::string m_ids; //! table name std::string m_table_name; //! incr key uint64_t m_incr_key; //! primary key std::string m_primary_key; std::size_t m_primary_key_index; }; table::table() :m_records_count(0), m_incr_key(0) { m_redis_client.connect(); m_redis_client.select(3); m_redis_client.sync_commit(); } void table::create(const std::string& table_name, const records_t& vec, const std::string& primary_key){ assert(m_records_count == 0); m_ids = "ids:" + table_name; m_table_name = table_name; m_records = vec; m_records_count = vec.size(); if(primary_key.empty()){ m_primary_key = vec[0]; m_primary_key_index = 0; } else { m_primary_key = primary_key; auto iter = std::find(vec.begin(), vec.end(), primary_key); if(iter == vec.end()){ LOG_FATAL << "no such key."; } m_primary_key_index = iter - vec.begin(); } } std::string table::incr_id(){ return std::move(std::to_string(m_incr_key++)); } std::string table::get_id_by_primary_key_value(const std::string& primary_key_value){ std::future<cpp_redis::reply> fu = m_redis_client.get(primary_key_value); m_redis_client.sync_commit(); cpp_redis::reply reply = fu.get(); if(!reply.is_null()){ return std::move(reply.as_string()); } LOG_DEBUG << "primary_key " << primary_key_value << " inexitences. return \"\"."; return ""; } bool table::insert(const entitys_t& vec){ assert(m_records_count != 0); assert(m_records_count == vec.size()); std::string get_id = incr_id(); // check whether the primary key already exists. std::string check_id = get_id_by_primary_key_value(vec[m_primary_key_index]); if(!check_id.empty()){ return false; } // redis string type primary key to id index. //LOG_DEBUG << m_table_name + ":" + m_records[m_primary_key_index] + ":" + vec[m_primary_key_index]; m_redis_client.set(m_table_name + ":" + m_records[m_primary_key_index] + ":" + vec[m_primary_key_index], get_id); // redis set type to save id. std::vector<std::string> id_vec = {get_id}; m_redis_client.sadd(m_ids, id_vec); // redis hash type to save records key-value. std::vector<std::pair<std::string, std::string>> entitys_pair_vec; for(std::size_t i = 0; i < m_records_count; i++){ entitys_pair_vec.emplace_back(make_pair(m_records[i], vec[i])); } m_redis_client.hmset(m_table_name + ":" + get_id, entitys_pair_vec); m_redis_client.sync_commit(); return true; } table::entitys_t table::get_entitys_by_primary_key_value(const std::string& primary_key_value){ std::string id = get_id_by_primary_key_value(m_table_name + ":" + m_records[m_primary_key_index] + ":" + primary_key_value); if(id == ""){ static entitys_t static_empty_entitys_vec; return static_empty_entitys_vec; LOG_ERROR << "no this entitys"; } entitys_t vec; std::future<cpp_redis::reply> reply = m_redis_client.hgetall(m_table_name + ":" + id); m_redis_client.sync_commit(); std::vector<cpp_redis::reply> v = reply.get().as_array(); auto iter = v.begin(); for(iter++; iter < v.end(); iter += 2){ //LOG_DEBUG << (*iter).as_string(); vec.emplace_back((*iter).as_string()); } return std::move(vec); } int main() { table accounts_info; table::records_t records_vec = {"id", "accounts", "password", "nick_name"}; accounts_info.create("sip:accounts_info", records_vec, "accounts"); table::entitys_t entitys_vec0 = {"0", "ailu_0", "123", "sola_0"}; accounts_info.insert(entitys_vec0); table::entitys_t entitys_vec1 = {"1", "ailu_1", "123", "sola_1"}; accounts_info.insert(entitys_vec1); table::entitys_t entitys_vec2 = {"2", "ailu_2", "123", "sola_2"}; accounts_info.insert(entitys_vec2); table::entitys_t entitys_vec3 = {"3", "ailu_3", "123", "sola_3"}; accounts_info.insert(entitys_vec3); table::entitys_t ailu_1_accounts = accounts_info.get_entitys_by_primary_key_value("ailu_1"); auto it = ailu_1_accounts.begin(); while(it != ailu_1_accounts.end()){ std::cout << *it << std::endl; it++; } getchar(); return 0; }
目前給出了redis增查簡單設計方法,更新和刪除也是經過redis的基本方法對應設計便可,這裏再也不詳述。 此外,能夠看出redis的數據庫設計仍是比較靈活的,如何設計出最適合咱們場景需求且高效的正是它難點所在。