Skip to content

Commit 3ea960b

Browse files
committed
Created 2016-11-11-using-firebase-as-a-json-cache.md
1 parent 7426afa commit 3ea960b

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

_posts/2016-11-11-using-firebase-as-a-json-cache.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: Using Firebase as a JSON Cache
55
---
66
## Using A JSON Cache service
77

8-
Good caching can reduce load on API's, save bandwidth costs, improve page load times and ultimately gives a better user experience. Any call can be cached on the server or in the browser, one way to reduce the load for a non dynamic server call for some JSON is to load the data into a JSON storage online as a intermediary stage. Your sever will essentially be shadowing this JSON cache and pushing to it when neccessary, therefore all the logic performed by your models needed to generate the JSON endpoint can be performed when needed and not every time a user requests the data.
8+
Good caching can reduce load on API's, save bandwidth costs, improve page load times and ultimately gives a better user experience. Any call can be cached on the server or in the browser, one way to reduce the load for a non dynamic server call for some JSON is to load the data into a JSON storage online as a intermediary stage. Your sever will essentially be shadowing this JSON cache and pushing to it when necessary, therefore all the logic performed by your models needed to generate the JSON endpoint can be performed when needed and not every time a user requests the data.
99

1010
I'll show an example using Firebase (https://firebase.google.com/), which is a service which allows you to read and write rapidly and concurrently to JSON storage online.
1111

@@ -23,7 +23,7 @@ service firebase.storage {
2323

2424
Make sure to set the rules, for debugging it can be a good idea to simply open up read and write for everyone, make sure to restrict them for production.
2525

26-
The next stage is for you server API to push to the Firebase Storage. Here is an example of PHP Laravel Middleware using the observer pattern to push JSON to firebase storage, the API is first calling itself to get the response and then caching it. The logic will be similiar for different languages and frameworks.
26+
The next stage is for you server API to push to the Firebase Storage. Here is an example of PHP Laravel Middleware using the observer pattern to push JSON to firebase storage, the API is first calling itself to get the response and then caching it. The logic will be similar for different languages and frameworks.
2727

2828
~~~
2929
$request = Request::create('/api/v2/sessiondata', 'GET');
@@ -35,10 +35,22 @@ $request = Request::create('/api/v2/sessiondata', 'GET');
3535
~~~
3636
3737
38-
Finally the front end application, whether you have a mobile app or a web application, needs to load the firebase plugin api and make a GET request to this https://herokuapp174.firebaseio.com/data.json. You may also need to authenticate correctly. The benefit of this system is that any encrytption or expensive database calls are no longer made by the server, it's essentially a backup data store which pushes whenever an admin updated a route with the middleware attached.
38+
Finally the front-end application, whether you have a mobile app or a web application, needs to load the firebase plugin API and make a GET request to this https://herokuapp174.firebaseio.com/data.json. You may also need to authenticate correctly. The benefit of this system is that any encryption or expensive database calls are no longer made by the server, it's essentially a backup data store which pushes whenever an admin updated a route with the middleware attached.
3939
40-
Here is an example for an Angular app consuming the FireBase Json cache:
40+
Here is an example for an Angular app consuming the Firebase JSON cache with CryptoJS decryption so that the data is at least obfuscated:
4141
4242
~~~
43-
43+
return $http.jsonp('https://herokuapp174.firebaseio.com/data.json?callback=JSON_CALLBACK')
44+
.success(function(response){
45+
46+
var responseString = JSON.stringify(response);
47+
var message = CryptoJS.AES.decrypt(responseString, '~' + appToken, {format: CryptoJSAesJson}).toString(CryptoJS.enc.Utf8);
48+
var data = JSON.parse(message);
49+
50+
data = FestivalTransformer.transformArray(festivals);
51+
52+
callback(data);
53+
54+
});
55+
~~~
4456

0 commit comments

Comments
 (0)