<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ja">
		<id>https://wiki.nonip.net/index.php?action=history&amp;feed=atom&amp;title=Gcp%2FFirebase%2FFirestore%2Fcache%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB%2Fasync</id>
		<title>Gcp/Firebase/Firestore/cacheモジュール/async - 変更履歴</title>
		<link rel="self" type="application/atom+xml" href="https://wiki.nonip.net/index.php?action=history&amp;feed=atom&amp;title=Gcp%2FFirebase%2FFirestore%2Fcache%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB%2Fasync"/>
		<link rel="alternate" type="text/html" href="https://wiki.nonip.net/index.php?title=Gcp/Firebase/Firestore/cache%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB/async&amp;action=history"/>
		<updated>2026-04-19T23:18:58Z</updated>
		<subtitle>このウィキのこのページに関する変更履歴</subtitle>
		<generator>MediaWiki 1.24.2</generator>

	<entry>
		<id>https://wiki.nonip.net/index.php?title=Gcp/Firebase/Firestore/cache%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB/async&amp;diff=9010&amp;oldid=prev</id>
		<title>Admin: ページの作成:「==asyncを使ったfunctionsのcacheサンプル== functions/index.js &lt;pre&gt; const functions = require('firebase-functions'); const admin = require('firebase-admin'); admi...」</title>
		<link rel="alternate" type="text/html" href="https://wiki.nonip.net/index.php?title=Gcp/Firebase/Firestore/cache%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB/async&amp;diff=9010&amp;oldid=prev"/>
				<updated>2019-08-19T05:06:21Z</updated>
		
		<summary type="html">&lt;p&gt;ページの作成:「==asyncを使ったfunctionsのcacheサンプル== functions/index.js &amp;lt;pre&amp;gt; const functions = require(&amp;#039;firebase-functions&amp;#039;); const admin = require(&amp;#039;firebase-admin&amp;#039;); admi...」&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新規ページ&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==asyncを使ったfunctionsのcacheサンプル==&lt;br /&gt;
functions/index.js&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
const functions = require('firebase-functions');&lt;br /&gt;
const admin = require('firebase-admin');&lt;br /&gt;
admin.initializeApp();&lt;br /&gt;
const cache = require('./cache.js');&lt;br /&gt;
exports.setCache = functions&lt;br /&gt;
  .region('asia-northeast1')&lt;br /&gt;
  .https.onRequest(async(request, response) =&amp;gt; {&lt;br /&gt;
    var id = request.query['id'];&lt;br /&gt;
    var cachedata = request.query['cachedata'];&lt;br /&gt;
    var retcachedata = await cache.setCache(admin, cachedata, id);&lt;br /&gt;
    response.send(&amp;quot;retcachedata=&amp;quot; + retcachedata);&lt;br /&gt;
});&lt;br /&gt;
exports.getCache = functions&lt;br /&gt;
  .region('asia-northeast1')&lt;br /&gt;
  .https.onRequest(async(request, response) =&amp;gt; {&lt;br /&gt;
    var id = request.query['id'];&lt;br /&gt;
    var cachedata = request.query['cachedata'];&lt;br /&gt;
    var cachelifesec = 60 * 60;&lt;br /&gt;
    if (request.query['cachelifesec'] != null) {&lt;br /&gt;
      cachelifesec = parseInt(request.query['cachelifesec']);&lt;br /&gt;
    }&lt;br /&gt;
    console.log('cachelifesec=' + cachelifesec);&lt;br /&gt;
    var cachedata = await cache.getCache(admin, id, cachelifesec);&lt;br /&gt;
    response.send(&amp;quot;cachedata=&amp;quot; + cachedata);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
functions/cache.js&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
Cache設定例&lt;br /&gt;
    const cache = require('./cache.js');&lt;br /&gt;
    cache.registerCallbackSetCache(onCacheResponseSetCache);&lt;br /&gt;
    var data = &amp;quot;hogehoge&amp;quot;;&lt;br /&gt;
    var data = await cache.setCache(admin, data, id);&lt;br /&gt;
    return data; // hogehoge&lt;br /&gt;
Cache取得例(functionをasyncで宣言しておく)&lt;br /&gt;
    const cache = require('./cache.js');&lt;br /&gt;
    console.log('cachelifesec=' + cachelifesec);&lt;br /&gt;
    var data = await cache.getCache(admin, id, cachelifesec);&lt;br /&gt;
    response.send(&amp;quot;data=&amp;quot; + data);&lt;br /&gt;
*/&lt;br /&gt;
exports.setCache = async function(admin, data, id) {&lt;br /&gt;
  var date = new Date() ;&lt;br /&gt;
  var unixtimems = date.getTime() ;&lt;br /&gt;
  var unixtime = Math.floor( unixtimems / 1000 ) ;&lt;br /&gt;
  console.log('unixtime=' + unixtime);&lt;br /&gt;
  const cache = {&lt;br /&gt;
    id: id,&lt;br /&gt;
    data: data,&lt;br /&gt;
    unixtime: unixtime,&lt;br /&gt;
  };&lt;br /&gt;
  var snapshot = await admin.firestore().collection('caches')&lt;br /&gt;
    .doc(id).set(cache);&lt;br /&gt;
  return &amp;quot;&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
exports.getCache = async function(admin, id, cachelifesec) {&lt;br /&gt;
    console.log('exports.getCache');&lt;br /&gt;
    var date = new Date() ;&lt;br /&gt;
    var unixtimems = date.getTime() ;&lt;br /&gt;
    var unixtime = Math.floor( unixtimems / 1000 ) ;&lt;br /&gt;
    console.log('unixtime=' + unixtime);&lt;br /&gt;
    var unixtimecachelifesec = unixtime - cachelifesec;&lt;br /&gt;
    console.log('cachelifesec=' + cachelifesec);&lt;br /&gt;
    console.log('unixtimecachelifesec=' +unixtimecachelifesec);&lt;br /&gt;
    var snapshot = await admin.firestore().collection('caches')&lt;br /&gt;
    .where('id', '==', id)&lt;br /&gt;
    .where('unixtime', '&amp;gt;', unixtimecachelifesec)&lt;br /&gt;
    .limit(1)&lt;br /&gt;
    .get();&lt;br /&gt;
    var caches = snapshot.docs.map(x =&amp;gt; x.data());&lt;br /&gt;
    for (let i = 0; i &amp;lt; caches.length; i++) {&lt;br /&gt;
       console.log(&amp;quot;ret data=&amp;quot; + caches[i].data);&lt;br /&gt;
       console.log(&amp;quot;getCache cache=&amp;quot; + caches[i].data);&lt;br /&gt;
       return caches[i].data;&lt;br /&gt;
    }&lt;br /&gt;
    console.log(&amp;quot;getCache nasi&amp;quot;);&lt;br /&gt;
    return &amp;quot;&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==httpアクセスで確認==&lt;br /&gt;
10秒でcacheが切れるサンプルurl&lt;br /&gt;
 // cache設定&lt;br /&gt;
 ttps://asia-northeast1-ほげほげ.cloudfunctions.net/setCache?id=hogeid&amp;amp;cachedata=dada1&lt;br /&gt;
 // cache取得&lt;br /&gt;
 ttps://asia-northeast1-ほげほげ.cloudfunctions.net/getCache?id=hogeid&amp;amp;cachelifesec=10&lt;br /&gt;
&lt;br /&gt;
cacheはid別にfirestoreに格納される。&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>