Skip to content

Commit e2be6fa

Browse files
committed
altered lru cache method to work interchangeably with redis async method
1 parent 6bdf086 commit e2be6fa

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

SSRtest/ModifiedReact.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var checkPropTypes = require('prop-types/checkPropTypes');
2727
var camelizeStyleName = require('fbjs/lib/camelizeStyleName');
2828
var stream = require('stream');
2929
var lru = require('lru-cache');
30+
var redis = require('redis');
3031

3132
/**
3233
* WARNING: DO NOT manually require this module.
@@ -2235,15 +2236,19 @@ var ReactDOMServerRenderer = function () {
22352236
}
22362237

22372238
// CACHING LOGIC: EXECUTES IF THE CHILD HAS A 'CACHE' PROP ON IT
2238-
if(child.props.cache){
2239+
if(child.props && child.props.cache){
22392240
const cacheKey = child.type.name + JSON.stringify(child.props);
2240-
// console.log(cache.get(cacheKey));
2241-
if (!cache.get(cacheKey)){
2242-
start[cacheKey] = out.length;
2243-
out += this.render(child, frame.context, frame.domNamespace);
2244-
} else {
2245-
out += cache.get(cacheKey);
2246-
}
2241+
2242+
// get method will run callback
2243+
cache.get(cacheKey, (err, reply) => {
2244+
if(reply){
2245+
out += reply;
2246+
} else {
2247+
start[cacheKey] = out.length;
2248+
out += this.render(child, frame.context, frame.domNamespace);
2249+
}
2250+
});
2251+
22472252
} else {
22482253
out += this.render(child, frame.context, frame.domNamespace);
22492254
}
@@ -2253,13 +2258,13 @@ var ReactDOMServerRenderer = function () {
22532258
}
22542259
}
22552260

2256-
for (let component in start) {
2261+
for (let cacheKey in start) {
22572262
let tagStack = [];
22582263
let tagStart;
22592264
let tagEnd;
22602265

22612266
do {
2262-
if (!tagStart) tagStart = start[component];
2267+
if (!tagStart) tagStart = start[cacheKey];
22632268
else tagStart = (out[tagEnd] === '<') ? tagEnd : out.indexOf('<', tagEnd)
22642269
tagEnd = out.indexOf('>', tagStart) + 1;
22652270
// Skip stack logic for void/self-closing elements
@@ -2271,7 +2276,9 @@ var ReactDOMServerRenderer = function () {
22712276
} while (tagStack.length !== 0);
22722277

22732278
// cache component by slicing 'out'
2274-
cache.set(component, out.slice(start[component], tagEnd));
2279+
cache.set(cacheKey, out.slice(start[cacheKey], tagEnd), (err, reply) => {
2280+
console.log(reply);
2281+
});
22752282
}
22762283
return out;
22772284
};
@@ -2617,8 +2624,10 @@ class ComponentCache {
26172624
});
26182625
}
26192626

2620-
get(cacheKey) {
2621-
return this.storage.get(cacheKey)
2627+
get(cacheKey, cb) {
2628+
let reply = this.storage.get(cacheKey);
2629+
// return reply;
2630+
cb(null,reply);
26222631
}
26232632

26242633
set(cacheKey, html) {

SSRtest/src/server/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import App from '../shared/App';
1010
// can pass in max-size, otherwise defaults to 1 million
1111
const cache = new ReactCC.ComponentCache();
1212
// import redis from 'redis';
13-
// const client = redis.createClient();
13+
// const cache = redis.createClient();
1414

1515
/**
1616
* @param clientStats Parameter passed by hot server middleware
@@ -19,8 +19,8 @@ export default ({ clientStats }) => async (req, res) => {
1919
const app = <App />;
2020
const start_cached = process.hrtime();
2121
// const appString = ReactCC.renderToStaticMarkup(app, cache);
22+
// console.log(cache);
2223
const appString = ReactCC.renderToString(app, cache);
23-
// const appString = ReactCC.renderToString(app, client);
2424
const end_cached = process.hrtime(start_cached);
2525
console.info(
2626
"Cached render time: %ds %dms",

SSRtest/src/shared/App.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export default class App extends Component {
1717
return (
1818
<div>
1919
<h1>THIS IS AN APP</h1>
20-
<Button />
20+
<Button cache/>
2121
<List cache/>
22-
<BlogPost />
22+
<BlogPost thing='haha'/>
2323
</div>
2424
);
2525
}

SSRtest/src/shared/BlogPost.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default class BlogPost extends Component {
1313
<div>
1414
<h1>This is a great blog post!</h1>
1515
<BlogContent />
16+
<p>{this.props.thing}</p>
1617
</div>
1718
);
1819
}

0 commit comments

Comments
 (0)