Redis modeling reservations

淺唱寂寞╮ 提交于 2021-01-24 12:54:56

问题


I'm working on a personal project to learn about Redis. I'm trying to use it as the database for managing a hotel. I'm trying to wrap my head around how to manage reservations.

The problem is, there are multiple rooms, and each room can have multiple reservations, storing date-from and date-to. I'm just unsure how i could model this, to efficiently be able to find an empty room for a given given period.

Currently, I've been thinking of storing reservations, for each seperate room, in sorted sets, but this way i'd have to iterate over the rooms one by one until i find a suitable one.

Any input is welcome. Thanks


回答1:


One way would be to use sets to represent a date range, where each set represents one day and is identified by an integer. Members of a set would then represent which rooms are available for reservation on that day. Since reservations start and end at the same time every day and any reservation can be measured in whole days you can represent any day as an integer. For example, using the Unix epoch (Jan 1 1970) as a start time, today (Apr 24 2016) would be 16915. Or in JavaScript:

Math.floor(new Date().getTime() / (1000 * 60 * 60 * 24));

You could then find which rooms are available for a given date range by doing a set intersection across the days in the input range. This gives you O(n * m) lookup time where n is the size of the smallest set and m is the number of sets. Since the set contains each room at most once this means n is bounded by the total number of rooms. For example:

var _ = require("lodash");

var dateToInt = function(date){
  return Math.floor(date.getTime() / (1000 * 60 * 60 * 24));
};

var roomsInRange = function(client, start, end, callback){
  client.sinter(_.range(dateToInt(start), dateToInt(end)), callback);
};

When a user reserves a room for a date range you would then remove that room number from each set in the date range.

var _ = require("lodash");

var dateToInt = function(date){
  return Math.floor(date.getTime() / (1000 * 60 * 60 * 24));
};

var reserveRoom = function(client, start, end, room, callback){
  var trx = client.multi();
  _.each(_.range(dateToInt(start), dateToInt(end), function(day){
    trx.srem(day, room);
  });
  trx.exec(callback);
};

This approach duplicates a lot of data in that each room is represented multiple times, but the size is bounded by the number of rooms and the largest date range you need to represent. For example, I wouldn't think users are allowed to book reservations 5 years in advance, nor are they allowed to book reservations in the past, which means you could clean up past entries in addition to limiting the upper bound on the input range. Given that keys are integers and rooms are likely also represented by integers I would be surprised if this took more than 1 or 2 MB for a year's worth of reservations.



来源:https://stackoverflow.com/questions/36825929/redis-modeling-reservations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!