最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Data storage in memory- Javascript - Stack Overflow

programmeradmin0浏览0评论

i want to store this kind of information in memory all the time.

app_id    admin_id    url           status
123       1           xyz       1
123       2           xyz       0
539       3           exmaple   1
876       4           new       1

My main concern is that i want the lookup to be easy and fast. Like at any point to query :

  1. get the url and status for a particular admin_id.
  2. get all admin_id for a particular app_id.

SO how do i store this information in memory.

i want to store this kind of information in memory all the time.

app_id    admin_id    url           status
123       1           xyz.       1
123       2           xyz.       0
539       3           exmaple.   1
876       4           new.       1

My main concern is that i want the lookup to be easy and fast. Like at any point to query :

  1. get the url and status for a particular admin_id.
  2. get all admin_id for a particular app_id.

SO how do i store this information in memory.

Share Improve this question edited Dec 26, 2011 at 18:38 Bhesh Gurung 51k23 gold badges95 silver badges143 bronze badges asked Dec 26, 2011 at 18:19 chiragchirag 431 silver badge5 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

You could do something as follows. Create an object for each row. And keep the references in and array byAdminId for admin-id, and an object byAppId for app-id, the add function does that in following. getByAppId and getByAdminId functions for retrieval of those objects.

var store = (function() {
    var byAdminId = [];
    var byAppId = {};

    return {
        add: function(appId, adminId, url, status) {
            var d = {
                appId: appId,
                adminId: adminId,
                url: url,
                status: status
            };
            if (appId in byAppId) { // You could also use byAppId.hasOwnProperty(appId) here
                byAppId[appId].push(d);
            } else {
                byAppId[appId] = [];
                byAppId[appId].push(d);
            }
            byAdminId[adminId-1] = d;
        },
        getByAppId: function(appId) {
            if (appId in byAppId) {
                return byAppId[appId];
            }
            return null;
        },
        getByAdminId: function(adminId) {
            return byAdminId[adminId-1];
        }
    };
})();

store.add(123, 1, "abc.", 1);
store.add(123, 9, "xyz.", 0);
store.add(539, 3, "exmaple.", 1);
store.add(876, 4, "new.", 1);

var objs = store.getByAppId(123);
for (var i = 0; i < objs.length; i++) {
    // access admin id as follows
    // objs[i].adminId
}

var obj = store.getByAdminId(1);
// access the url and status as follows
// obj.url
// obj.status

If you have your items in an array called a, you could use a couple of dictionaries:

var i;

var app_id_index = {};
var admin_id_index = {};
for (i = 0; i < a.length; i++) {
    app_id_index[a[i].app_id] = a[i];
    admin_id_index[a[i].admin_id] = a[i];
}

see this
http://dojotoolkit/documentation/tutorials/1.6/intro_dojo_store/
http://dojotoolkit/reference-guide/dojo/data.html#dojo-data

发布评论

评论列表(0)

  1. 暂无评论