Skip to content

Commit 1fa015e

Browse files
committed
Initial commit
0 parents  commit 1fa015e

File tree

7 files changed

+523
-0
lines changed

7 files changed

+523
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Feedhenry Tutorial - Mash Hash Cache
2+
====================================
3+
This is the source code relating to the Feedhenry Mash Hash Cache Tutorial.
4+
Documentation for using the code is available at the [Feedhenry Docs Site](http://docs.feedhenry.com/getting-started/training-labs/mash-hash-cache/)

client/default/index.html

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<style>
2+
html,body {
3+
width:100%;
4+
height:100%;
5+
}
6+
body {
7+
font-family:arial, sans-serif;
8+
font-size:12px;
9+
background-color:#eee;
10+
padding:1em;
11+
}
12+
p {
13+
font-weight:bold;
14+
}
15+
span {
16+
font-weight:normal;
17+
}
18+
</style>
19+
20+
<input id="mashup_button" type="button" value="Get Mashup from Server">
21+
22+
23+
<div>
24+
<h2>Mashup Result</h2>
25+
<p>My Hash: <span id="mashup_myhash"></span></p>
26+
<p>Servers Hash: <span id="mashup_serverhash"></span></p>
27+
<p>Local Cache Used: <span id="mashup_cached"></span></p>
28+
<p>No. Entries: <span id="mashup_entries"></span></p>
29+
<p>Data: <span id="mashup_data"></span></p>
30+
</div>
31+
32+
<script type="text/javascript" src="js/script.js"></script>
33+

client/default/js/script.js

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Wait until the everythings loaded before setting up the app
2+
$fh.ready(function () {
3+
var mashup_button = $('#mashup_button');
4+
5+
mashup_button.bind('click', function (e) {
6+
mashup_button.attr('disabled', true).val('loading...');
7+
// Get our locally stored hash of the data if we have one
8+
$fh.data({
9+
act: 'load',
10+
key: 'mashup_hash'
11+
}, function (res) {
12+
// We have a hash, lets send this along to the server
13+
if (null !== res.val) {
14+
var hash = res.val;
15+
doMashup(hash);
16+
}
17+
else {
18+
// No hash, so we haven't gotten any data from server yet
19+
// Lets just call the server and get whatever data it has
20+
doMashup();
21+
}
22+
}, function (error) {
23+
alert(error);
24+
enableMashupButton();
25+
});
26+
});
27+
});
28+
29+
30+
function doMashup(hash) {
31+
hash = 'undefined' === typeof hash ? '' : hash;
32+
33+
// Call the server side function to get the mashup data
34+
$fh.act({
35+
act: 'getMashup',
36+
req: {
37+
hash: hash,
38+
timestamp: new Date().getTime()
39+
}
40+
}, function (res) {
41+
var mashup = res;
42+
43+
// Check if our hash's match, so we can use the locally cached data
44+
if (hash === mashup.hash) {
45+
// we already have the latest data, lets pull it from local storage
46+
$fh.data({
47+
act: 'load',
48+
key: hash
49+
}, function (res) {
50+
if (null !== res.val) {
51+
// Parse out the data and show it
52+
var data = JSON.parse(res.val);
53+
updateMashup(hash, data);
54+
}
55+
else {
56+
// Local data doesn't exist. Notify user and get fresh data instead
57+
alert('Server said hashes match, but no local data found for ' + hash
58+
+ '. Doing a fresh mashup call to repopulate our local data. (no hash parm passed to ensure server call)');
59+
doMashup();
60+
}
61+
}, function (error) {
62+
alert('Error retrieving mashup data from local storage:' + error);
63+
enableMashupButton();
64+
});
65+
}
66+
else {
67+
// Hash's don't match, so server has included the newest data to use
68+
// Lets save it and update our hash value
69+
$fh.data({
70+
act: 'save',
71+
key: 'mashup_hash',
72+
val: mashup.hash
73+
}, function (res) {
74+
// hash saved - so now save the new mashup data returned
75+
$fh.data({
76+
act: 'save',
77+
key: mashup.hash,
78+
val: JSON.stringify(mashup)
79+
}, function (res) {
80+
// mashup data saved
81+
}, function (error) {
82+
alert(error);
83+
enableMashupButton();
84+
});
85+
}, function (error) {
86+
alert(error);
87+
enableMashupButton();
88+
});
89+
// No need to wait for asynchronous saving above to finish. Lets go ahead
90+
// and show the latest mashup data
91+
updateMashup(hash, mashup);
92+
}
93+
}, function (code,errorprops,params) {
94+
// Something went wrong with server side function call, let's alert the user
95+
alert('Failed to get mashup from server. Error:' + code);
96+
enableMashupButton();
97+
});
98+
}
99+
100+
101+
function updateMashup(hash, mashup) {
102+
$('#mashup_myhash').text(hash);
103+
$('#mashup_serverhash').text(mashup.hash);
104+
$('#mashup_cached').text(hash === mashup.hash);
105+
$('#mashup_entries').text(mashup.data.length);
106+
$('#mashup_data').text(JSON.stringify(mashup.data));
107+
108+
enableMashupButton();
109+
}
110+
111+
function enableMashupButton() {
112+
$('#mashup_button').removeAttr('disabled').val('Get Mashup from Server');
113+
}

cloud/main.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Server-side function. It will be invoked once client use $fh.act to call this function.
3+
* It will return whether client cached data is up to date. If no, the current data and hash will be retrived as well.
4+
* @param hash client-side cached hash value which is parsed as parameter. Use $params.hash to retrive it.
5+
*/
6+
function getMashup() {
7+
8+
var response = {};
9+
10+
// Check the cloud cache to see if we have data
11+
var cached = readCache();
12+
13+
// No cahced data in cloud
14+
if( ""=== cached ) {
15+
16+
// Get data from remote web services
17+
var data = doMashUp();
18+
19+
// Create MD5 hash of data
20+
var hash = createHash($fh.stringify(data));
21+
22+
// Store data and hash in cloud cache
23+
doCache(hash, data);
24+
25+
// Build response object
26+
response = {'data': data, 'hash':hash, 'cached':false};
27+
}
28+
else {
29+
//transform cached data from string type to object type
30+
cached=$fh.parse(cached);
31+
// Check if client hash value present & correct
32+
if( $params.hash && $params.hash === cached.hash ) {
33+
// Don't need to send data back to client as hash is up to date
34+
response = {'hash':$params.hash, 'cached':true};
35+
}
36+
else {
37+
// Hash value from client missing or incorrect, return cached cloud data
38+
response = cached;
39+
}
40+
}
41+
42+
return response;
43+
}

0 commit comments

Comments
 (0)