facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「==functionsのサンプル== functions/index.js <pre> const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(); exp...」)
 
 
(同じ利用者による、間の11版が非表示)
行1: 行1:
==functionsのサンプル==
+
[[Gcp/Firebase/Firestore/cacheモジュール/基本]]
functions/index.js
+
<pre>
+
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 data = request.query['data'];
+
    //var unixtime = request.query['unixtime'];
+
    const cache = require('./cache.js');
+
    cache.registerCallback(onCacheResponse);
+
    cache.setCache(admin, data, id);
+
    function onCacheResponse(data) {
+
      response.send("data=" + data);
+
    }
+
});
+
  
exports.getCache = functions
+
[[Gcp/Firebase/Firestore/cacheモジュール/async]]
  .region('asia-northeast1')
+
  .https.onRequest((request, response) => {
+
    var id = request.query['id'];
+
    var data = request.query['data'];
+
    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(data) {
+
      response.send("data=" + data);
+
    }
+
});
+
</pre>
+
 
+
functions/cache.js
+
<pre>
+
/*
+
Cache設定例
+
    const cache = require('./cache.js');
+
    cache.registerCallback(onCacheResponse);
+
    cache.setCache(admin, data, id);
+
    function onCacheResponse(data) {
+
      response.send("data=" + data);
+
    }
+
Cache取得例
+
    const cache = require('./cache.js');
+
    cache.registerCallback(onCacheResponse);
+
    console.log('cachelifesec=' + cachelifesec);
+
    cache.getCache(admin, id, cachelifesec);
+
    function onCacheResponse(data) {
+
      response.send("data=" + data);
+
    }
+
*/
+
 
+
var mCallback;
+
exports.registerCallback = function(callback) {
+
    mCallback = callback;
+
}
+
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) =>
+
  {
+
    mCallback(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);
+
      mCallback(caches[i].data);
+
      return;
+
    }
+
    mCallback("");
+
  });
+
}
+
</pre>
+
 
+
==httpアクセスで確認==
+
10秒でcacheが切れるように
+
https://asia-northeast1-flicklearning-aec6f.cloudfunctions.net/setCache?id=hogeid&data=dada1
+
https://asia-northeast1-flicklearning-aec6f.cloudfunctions.net/getCache?id=hogeid&cachelifesec=10
+
 
+
cacheはid別にfirestoreに格納される。
+

2019年8月19日 (月) 14:02時点における最新版

Gcp/Firebase/Firestore/cacheモジュール/基本

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