facebook twitter hatena line email

「Gcp/Firebase/Firestore/cacheモジュール」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
行51: 行51:
 
     cache.registerCallback(onCacheResponse);
 
     cache.registerCallback(onCacheResponse);
 
     cache.setCache(admin, cachedata, id);
 
     cache.setCache(admin, cachedata, id);
     function onCacheResponse(cachedata) {
+
     const onCacheResponseSetCache = function(data) {
       response.send("cachedata=" + cachedata);
+
       return data; // hogehoge
 
     }
 
     }
 +
    console.log(cache.getResCallbackSetCache()); // hogehoge
 
Cache取得例
 
Cache取得例
 
     const cache = require('./cache.js');
 
     const cache = require('./cache.js');
行59: 行60:
 
     console.log('cachelifesec=' + cachelifesec);
 
     console.log('cachelifesec=' + cachelifesec);
 
     cache.getCache(admin, id, cachelifesec);
 
     cache.getCache(admin, id, cachelifesec);
     function onCacheResponse(cachedata) {
+
     const onCacheResponseGetCache = function(data) {
       response.send("cachedata=" + cachedata);
+
       response.send("data=" + data);
 
     }
 
     }
 +
    console.log(cache.getResCallbackGetCache()); // hogehoge
 
*/
 
*/
  
var mCallback;
+
var mCallbackSetCache;
exports.registerCallback = function(callback) {
+
var mResCallbackSetCache = "";
     mCallback = callback;
+
var mCallbackGetCache;
 +
var mResCallbackGetCache = "";
 +
exports.registerCallbackSetCache = function(callback) {
 +
     mCallbackSetCache = callback;
 +
}
 +
exports.getResCallbackSetCache = function() {
 +
    return mResCallbackSetCache;
 +
}
 +
exports.registerCallbackGetCache = function(callback) {
 +
    mCallbackGetCache = callback;
 +
}
 +
exports.getResCallbackGetCache = function() {
 +
    console.log("getResCallbackGetCache mResCallbackGetCache=" + mResCallbackGetCache);
 +
    return mResCallbackGetCache;
 
}
 
}
 
exports.setCache = function(admin, data, id) {
 
exports.setCache = function(admin, data, id) {
行82: 行97:
 
     .then((snapshot) =>
 
     .then((snapshot) =>
 
   {
 
   {
    mCallback(data);
+
      if (mCallbackSetCache != null) {
 +
        mResCallbackSetCache = mCallbackSetCache(data);
 +
      }
 
   });
 
   });
 
}
 
}
行103: 行120:
 
     for (let i = 0; i < caches.length; i++) {
 
     for (let i = 0; i < caches.length; i++) {
 
       console.log("ret data=" + caches[i].data);
 
       console.log("ret data=" + caches[i].data);
       mCallback(caches[i].data);
+
       if (mCallbackGetCache != null) {
 +
        mResCallbackGetCache = mCallbackGetCache(caches[i].data);
 +
        console.log("getCache ari mResCallbackGetCache=" + mResCallbackGetCache);
 +
      }
 
       return;
 
       return;
 
     }
 
     }
     mCallback("");
+
     if (mCallbackGetCache != null) {
 +
      mResCallbackGetCache = mCallbackGetCache("");
 +
      console.log("getCache nasi mResCallbackGetCache=" + mResCallbackGetCache);
 +
    }
 
   });
 
   });
 
}
 
}

2019年8月11日 (日) 11:06時点における版

functionsのサンプル

  • id別にfirestoreのcahesコレクションへ文字列をキャッシュする
  • cache保持時間を設定できるように

cachesのテーブル構成例

id string | data string | unixtime int
"hogeid" | "hoge" | 123423451

functions/index.js

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.setCache = functions
  .region('asia-northeast1')
  .https.onRequest((request, response) => {
    var id = request.query['id'];
    var cachedata = request.query['cachedata'];
    const cache = require('./cache.js');
    cache.registerCallback(onCacheResponse);
    cache.setCache(admin, cachedata, id);
    function onCacheResponse(cachedata) {
       response.send("cachedata=" + cachedata);
    }
});

exports.getCache = functions
  .region('asia-northeast1')
  .https.onRequest((request, response) => {
    var id = request.query['id'];
    var cachedata = request.query['cachedata'];
    var cachelifesec = 0;
    if (request.query['cachelifesec'] != null) {
      cachelifesec = parseInt(request.query['cachelifesec']);
    }
    const cache = require('./cache.js');
    cache.registerCallback(onCacheResponse);
    console.log('cachelifesec=' + cachelifesec);
    cache.getCache(admin, id, cachelifesec);
    function onCacheResponse(cachedata) {
       response.send("cachedata=" + cachedata);
    }
});

functions/cache.js

/*
Cache設定例
    const cache = require('./cache.js');
    cache.registerCallback(onCacheResponse);
    cache.setCache(admin, cachedata, id);
    const onCacheResponseSetCache = function(data) {
       return data; // hogehoge
    }
    console.log(cache.getResCallbackSetCache()); // hogehoge
Cache取得例
    const cache = require('./cache.js');
    cache.registerCallback(onCacheResponse);
    console.log('cachelifesec=' + cachelifesec);
    cache.getCache(admin, id, cachelifesec);
    const onCacheResponseGetCache = function(data) {
       response.send("data=" + data);
    }
    console.log(cache.getResCallbackGetCache()); // hogehoge
*/

var mCallbackSetCache;
var mResCallbackSetCache = "";
var mCallbackGetCache;
var mResCallbackGetCache = "";
exports.registerCallbackSetCache = function(callback) {
    mCallbackSetCache = callback;
}
exports.getResCallbackSetCache = function() {
    return mResCallbackSetCache;
}
exports.registerCallbackGetCache = function(callback) {
    mCallbackGetCache = callback;
}
exports.getResCallbackGetCache = function() {
    console.log("getResCallbackGetCache mResCallbackGetCache=" + mResCallbackGetCache);
    return mResCallbackGetCache;
}
exports.setCache = 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,
  };
  admin.firestore().collection('caches')
    .doc(id).set(cache)
    .then((snapshot) =>
  {
      if (mCallbackSetCache != null) {
        mResCallbackSetCache = mCallbackSetCache(data);
      }
  });
}
exports.getCache = function(admin, id, cachelifesec) {
    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);
  admin.firestore().collection('caches')
    .where('id', '==', id)
    .where('unixtime', '>', unixtimecachelifesec)
    .limit(1)
    .get()
    .then((snapshot) =>
  {
    var caches = snapshot.docs.map(x => x.data());
    for (let i = 0; i < caches.length; i++) {
       console.log("ret data=" + caches[i].data);
       if (mCallbackGetCache != null) {
         mResCallbackGetCache = mCallbackGetCache(caches[i].data);
         console.log("getCache ari mResCallbackGetCache=" + mResCallbackGetCache);
       }
       return;
    }
    if (mCallbackGetCache != null) {
      mResCallbackGetCache = mCallbackGetCache("");
      console.log("getCache nasi mResCallbackGetCache=" + mResCallbackGetCache);
    }
  });
}

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に格納される。