官术网_书友最值得收藏!

Caching

Yet another place where server-side programming can be used is to cache values, which are expensive to compute. The following is the basic pattern here:

  1. Check whether the value is cached.
  2. If it isn't, or the value is too old, compute and cache it.
  3. Return the cached value.

For example, calculating the sales for a company is the perfect item to cache. Perhaps, a large retail company has 1,000 stores with potentially millions of individual sales' transactions per day. If the corporate headquarters is looking for sales' trends, it is much more efficient if the daily sales numbers are precalculated at the store level instead of summing up millions of daily transactions.

If the value is simple, such as looking up a user's information from a single table based on the user ID, you don't need to do anything. The value gets cached in PostgreSQL's internal page cache, and all lookups to it are so fast that even on a very fast network, most of the time is spent doing the lookups in the network and not in the actual lookup. In such a case, getting data from a PostgreSQL database is as fast as getting it from any other in-memory cache (such as memcached) but without any extra overhead in managing the cache.

Another use case of caching is to implement materialized views. These are views that are precomputed only when required, not every time one selects data from the view. Some SQL databases have materialized views as separate database objects, but in the PostgreSQL versions prior to 9.3, you have to do it yourself using other database features to automate the whole process.

主站蜘蛛池模板: 铁力市| 云阳县| 玛沁县| 华亭县| 佛冈县| 长阳| 黑山县| 含山县| 常山县| 雷山县| 平利县| 望都县| 徐州市| 黄浦区| 南城县| 缙云县| 崇明县| 徐州市| 伊金霍洛旗| 贵港市| 邵东县| 随州市| 崇信县| 金秀| 松阳县| 巴林右旗| 南涧| 平顶山市| 荣成市| 乌拉特中旗| 定西市| 南雄市| 晋江市| 霸州市| 米脂县| 永嘉县| 阿拉善盟| 揭东县| 辽阳县| 玛多县| 上杭县|