ReactCC is a component-level caching library for rendering React components on the server.
- Use any of React's four server-side rendering methods
- Cache simple components or templates
- Choose from three cache implementations (LRU, Redis, or Memcached)
Using npm:
$ npm install reactcc
Instantiate a cache and pass it into any rendering method as a second argument. Wherever you would use ReactDOM.renderToString, use ReactCC.renderToString.
const ReactCC = require("reactcc");
const cache = ReactCC.ComponentCache();
ReactCC.renderToString(<App />, cache>)
// ...
To flag a component for caching, simply add a 'cache' property to it. To create a cache template, add both 'cache' and 'templatized', along with an array of props to templatize.
export default class App extends Component {
render() {
return (
<div>
<ComponentNotToBeCached />
<ComponentToCache cache />
<ComponentToTemplatize cache templatized='props to templatize' />
</div>
);
}
}
// ...
ReactCC provides its own cache implementation as well as support for Redis and Memcached. Simply create your preferred cache and pass it into one of the rendering methods.
ReactCC LRU Cache Example:
const ReactCC = require("reactcc");
const cache = ReactCC.ComponentCache();
ReactCC.renderToString(<App />, cache);
Redis Example:
const ReactCC = require("reactcc");
const redis = require("redis");
const cache = redis.createClient();
ReactCC.renderToString(<App />, cache);
Memcached Example:
const ReactCC = require("reactcc");
const Memcached = require("memcached");
const cache = new Memcached(server location, options);
// Make sure to pass in the lifetime of the data (in seconds) as a number.
ReactCC.renderToString(<App />, cache, 1000);
Insert description and implementation here
ReactCC gives you access to all four of React 16's server-side rendering methods, as well as additional functionality. ReactCC methods are described below.
size
: (Optional) An integer representing the maximum size (in characters) of the cache. Defaults to 1 million.
Example:
const cache = new ReactCC.ComponentCache();
component
: The React component being rendered.cache
: The component cachememLife
: (Only if using Memcached) A number representing the lifetime (in seconds) of each Memcached entry. Defaults to 0.
Example:
ReactCC.renderToString(<App />, cache);
component
: The React component being rendered.cache
: The component cachememLife
: (Only if using Memcached) An integer representing the lifetime (in seconds) of each Memcached entry. Defaults to 0.
Example:
ReactCC.renderToStaticMarkup(<App />, cache);
component
: The React component being rendered.cache
: The component cachememLife
: (Only if using Memcached) An integer representing the lifetime (in seconds) of each Memcached entry. Defaults to 0.
Example:
ReactCC.renderToNodeStream(<App />, cache);
component
: The React component being rendered.cache
: The component cachememLife
: (Only if using Memcached) An integer representing the lifetime (in seconds) of each Memcached entry. Defaults to 0.
Example:
ReactCC.renderToStaticNodeStream(<App />, cache);