facebook twitter hatena line google mixi email

Gcp/Firebase/Firestore/cacheモジュール/async

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

asyncを使ったfunctionsのcacheサンプル

functions/index.js

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const cache = require('./cache.js');
exports.setCache = functions
  .region('asia-northeast1')
  .https.onRequest(async(request, response) => {
    var id = request.query['id'];
    var cachedata = request.query['cachedata'];
    var retcachedata = await cache.setCache(admin, cachedata, id);
    response.send("retcachedata=" + retcachedata);
});
exports.getCache = functions
  .region('asia-northeast1')
  .https.onRequest(async(request, response) => {
    var id = request.query['id'];
    var cachedata = request.query['cachedata'];
    var cachelifesec = 60 * 60;
    if (request.query['cachelifesec'] != null) {
      cachelifesec = parseInt(request.query['cachelifesec']);
    }
    console.log('cachelifesec=' + cachelifesec);
    var cachedata = await cache.getCache(admin, id, cachelifesec);
    response.send("cachedata=" + cachedata);
});

functions/cache.js

/*
Cache設定例
    const cache = require('./cache.js');
    cache.registerCallbackSetCache(onCacheResponseSetCache);
    var data = "hogehoge";
    var data = await cache.setCache(admin, data, id);
    return data; // hogehoge
Cache取得例(functionをasyncで宣言しておく)
    const cache = require('./cache.js');
    console.log('cachelifesec=' + cachelifesec);
    var data = await cache.getCache(admin, id, cachelifesec);
    response.send("data=" + data);
*/
exports.setCache = async function(admin, data, id) {
  var date = new Date() ;
  var unixtimems = date.getTime() ;
  var unixtime = Math.floor( unixtimems / 1000 ) ;
  console.log('unixtime=' + unixtime);
  const cache = {
    id: id,
    data: data,
    unixtime: unixtime,
  };
  var snapshot = await admin.firestore().collection('caches')
    .doc(id).set(cache);
  return "";
}
exports.getCache = async function(admin, id, cachelifesec) {
    console.log('exports.getCache');
    var date = new Date() ;
    var unixtimems = date.getTime() ;
    var unixtime = Math.floor( unixtimems / 1000 ) ;
    console.log('unixtime=' + unixtime);
    var unixtimecachelifesec = unixtime - cachelifesec;
    console.log('cachelifesec=' + cachelifesec);
    console.log('unixtimecachelifesec=' +unixtimecachelifesec);
    var snapshot = await admin.firestore().collection('caches')
    .where('id', '==', id)
    .where('unixtime', '>', unixtimecachelifesec)
    .limit(1)
    .get();
    var caches = snapshot.docs.map(x => x.data());
    for (let i = 0; i < caches.length; i++) {
       console.log("ret data=" + caches[i].data);
       console.log("getCache cache=" + caches[i].data);
       return caches[i].data;
    }
    console.log("getCache nasi");
    return "";
}

httpアクセスで確認

10秒でcacheが切れるサンプルurl

// cache設定
ttps://asia-northeast1-ほげほげ.cloudfunctions.net/setCache?id=hogeid&cachedata=dada1
// cache取得
ttps://asia-northeast1-ほげほげ.cloudfunctions.net/getCache?id=hogeid&cachelifesec=10

cacheはid別にfirestoreに格納される。