Redis建模保留 [英] Redis modeling reservations

查看:64
本文介绍了Redis建模保留的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个个人项目,以了解Redis.我正在尝试将其用作管理酒店的数据库.我正在努力处理如何管理预订.

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.

问题是,有多个房间,每个房间可以有多个预订,存储date-from和date-to.我只是不确定如何建模,以有效地找到给定时间的空房间.

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.

欢迎任何输入.谢谢

推荐答案

一种方法是使用集合表示日期范围,其中每个集合表示一天,并由一个整数标识.然后,一组成员将代表当天可以预订哪些房间.由于预订每天都在同一时间开始和结束,并且任何预订都可以一整天进行衡量,因此您可以将任意一天表示为整数.例如,以Unix时代(1970年1月1日)为开始时间,今天(2016年4月24日)将为16915.或者在JavaScript中:

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));

然后通过在输入范围内的各天之间进行固定的相交,可以找到在给定日期范围内可用的房间. 这为您提供O(n * m)查找时间,其中n是最小集合的大小,而m是套数.由于集合最多包含每个房间一次,这意味着n受房间总数限制.例如:

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);
};

此方法重复了很多数据,因为每个房间都被表示多次,但是大小受到房间数量和您需要表示的最大日期范围的限制.例如,我认为用户不允许提前5年预订,也不允许他们过去预订,这意味着除了限制输入范围的上限外,您还可以清理过去的条目.假设键是整数,而房间也可能用整数表示,那么如果一年的预订花费了1或2 MB以上的空间,我会感到惊讶.

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.

这篇关于Redis建模保留的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆