All Projects → rookLab → React Component Caching

rookLab / React Component Caching

Speedier server-side rendering with component caching in React 16

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Component Caching

Easycaching
💥 EasyCaching is an open source caching library that contains basic usages and some advanced usages of caching which can help us to handle caching more easier!
Stars: ✭ 1,047 (+186.85%)
Mutual labels:  redis, caching, memcached
Cachemanager
CacheManager is an open source caching abstraction layer for .NET written in C#. It supports various cache providers and implements many advanced features.
Stars: ✭ 2,049 (+461.37%)
Mutual labels:  redis, caching, memcached
Scrapbook
PHP cache library, with adapters for e.g. Memcached, Redis, Couchbase, APC(u), SQL and additional capabilities (e.g. transactions, stampede protection) built on top.
Stars: ✭ 279 (-23.56%)
Mutual labels:  redis, caching, memcached
Sequelize Transparent Cache
Simple to use and universal cache layer for Sequelize
Stars: ✭ 137 (-62.47%)
Mutual labels:  redis, caching, memcached
Ninja Mutex
Mutex implementation for PHP
Stars: ✭ 180 (-50.68%)
Mutual labels:  redis, memcached
Ansible Role Redis
Ansible Role - Redis
Stars: ✭ 176 (-51.78%)
Mutual labels:  redis, memcached
Zend Diagnostics
Universal set of diagnostic tests for PHP applications.
Stars: ✭ 192 (-47.4%)
Mutual labels:  redis, memcached
Zapi
基于swoole的异步轻量级api框架,内部封装全套mysql、redis、mongo、memcached异步客户端,可以轻松start、reload、stop,加入数据库的查询模块,框架已经封装好近乎同步写法,底层异步调用。现已支持异步mysql、异步redis、异步http请求.
Stars: ✭ 245 (-32.88%)
Mutual labels:  redis, memcached
Cachego
Golang Cache component - Multiple drivers
Stars: ✭ 148 (-59.45%)
Mutual labels:  redis, memcached
Lnmp
LEMP stack/LAMP stack/LNMP stack installation scripts for CentOS/Redhat Debian and Ubuntu
Stars: ✭ 2,488 (+581.64%)
Mutual labels:  redis, memcached
webuntis
A API library that makes it easy to access the Webuntis JSON RPC 2.0 API
Stars: ✭ 22 (-93.97%)
Mutual labels:  caching, memcached
Simple Settings
A simple way to manage your project settings.
Stars: ✭ 165 (-54.79%)
Mutual labels:  redis, memcached
Zhong
Reliable, distributed cron.
Stars: ✭ 281 (-23.01%)
Mutual labels:  redis, memcached
Senparc.co2net
支持 .NET Framework & .NET Core 的公共基础扩展库
Stars: ✭ 289 (-20.82%)
Mutual labels:  redis, memcached
Phpfastcache
A high-performance backend cache system. It is intended for use in speeding up dynamic web applications by alleviating database load. Well implemented, it can drops the database load to almost nothing, yielding faster page load times for users, better resource utilization. It is simple yet powerful.
Stars: ✭ 2,171 (+494.79%)
Mutual labels:  redis, memcached
Oneinstack
OneinStack - A PHP/JAVA Deployment Tool
Stars: ✭ 1,983 (+443.29%)
Mutual labels:  redis, memcached
Flipper
🐬 Beautiful, performant feature flags for Ruby.
Stars: ✭ 2,732 (+648.49%)
Mutual labels:  redis, memcached
Zhttp
基于swoole的异步轻量级web框架,内部封装协程异步非阻塞全套mysql、redis、mongo、memcached连接池,可以轻松start、reload、stop,加入数据库的查询模块,框架已经封装好近乎同步写法,底层异步调用
Stars: ✭ 131 (-64.11%)
Mutual labels:  redis, memcached
Gokv
Simple key-value store abstraction and implementations for Go (Redis, Consul, etcd, bbolt, BadgerDB, LevelDB, Memcached, DynamoDB, S3, PostgreSQL, MongoDB, CockroachDB and many more)
Stars: ✭ 314 (-13.97%)
Mutual labels:  redis, memcached
kdk memcached object cache
Object cache driver for Memcached in WordPress (based on Memcached Redux)
Stars: ✭ 20 (-94.52%)
Mutual labels:  caching, memcached

React Component Caching

Overview

React Component Caching is a component-level caching library for faster server-side rendering with React 16.

  • Use any of React's four server-side rendering methods. Rendering is asynchronous.
  • Cache components using a simple or template strategy.
  • Choose from three cache implementations (LRU, Redis, or Memcached).

Installation

Using npm:

$ npm install react-component-caching

Usage

In Node rendering server:

Instantiate a cache and pass it to any rendering method (renderToString, renderToStaticMarkup, renderToNodeStream, or renderToStaticNodeStream) as a second argument. Wherever you would use ReactDOM.renderToString, use ReactCC.renderToString.

Note: All of these methods are asynchronous, and return a promise. To use them, await the response before rendering

const ReactCC = require("react-component-caching");
const cache = new ReactCC.ComponentCache();

app.get('/example', async (req,res) => {
    const renderString = await ReactCC.renderToString(<App />, cache);
    res.send(renderString);
});

// ...

In React app:

To flag a component for caching, simply add a cache property to it.

export default class App extends Component {
    render() {
        return (
            <div>
                <ComponentNotToBeCached />
                <ComponentToCache cache />
            </div>
        );
    }
}
// ...

Templatizing Cached Components

The example above employs a simple caching strategy: a rendered component is saved with its prop values. Each time the component is rendered with different prop values, a separate copy is saved to the cache. If a component is frequently rendered with different prop values, you may prefer to cache a template of the component to save space in the cache. The template strategy stores a version of the component with placeholders (e.g. {{0}}, {{1}}) in place of actual prop values.

To create a cache template, add both cache and templatized to the component along with an array of props to templatize. Templatized props should have string or number values. Be aware that templates are not currently supported with the renderToNodeStream or renderToStaticNodeStream methods.

export default class App extends Component {
    render() {
        return (
            <div>
                <ComponentNotToBeCached />
                <ComponentToCache cache />
                <ComponentToTemplatize
                    templatizedProp1="value1"
                    templatizedProp2="value2"
                    nonTemplatizedProp="anotherValue"
                    cache
                    templatized={["templatizedProp1", "templatizedProp2"]} />
            </div>
        );
    }
}
// ...

Streaming HTML Markup

To use streaming on the server side, use either the renderToStaticNodeStream or renderToNodeStream function. Both streaming option works with caching, but not yet compatible with templatization. To use the streaming functions, simply pass in these 5 arguments: ( component: The React component being rendered cache: The component cache object res: The response object that Express provides htmlStart: Start of html markup in string form htmlEnd: End of html markup in string form ). The benefit that comes with streaming is faster time to first byte, which translates to faster viewing of page content.

Cache Options

React Component Caching 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.

Standard (LRU) Cache Example:

const ReactCC = require("react-component-caching");
const cache = new ReactCC.ComponentCache();

Redis Example:

const ReactCC = require("react-component-caching");
const redis = require("redis");
const cache = redis.createClient();

Memcached Example:

const ReactCC = require("react-component-caching");
const Memcached = require("memcached");
const cache = new Memcached(server location, options);

// If using Memcached, make sure to pass in the lifetime of the data (in seconds) as a number.
ReactCC.renderToString(<App />, cache, 1000);

API

React Component Caching

React Component Caching gives you access to all four of React 16's server-side rendering methods, as well as additional functionality. Available methods are described below.

ComponentCache

  • size: (Optional) An integer representing the maximum size (in characters) of the cache. Defaults to 1 million.

Example:

const cache = new ReactCC.ComponentCache();

renderToString

  • component: The React component being rendered
  • cache: The component cache
  • memLife: (Only if using Memcached) A number representing the lifetime (in seconds) of each Memcached entry. Defaults to 0.

Example:

ReactCC.renderToString(<App />, cache);

renderToStaticMarkup

  • component: The React component being rendered
  • cache: The component cache
  • memLife: (Only if using Memcached) An integer representing the lifetime (in seconds) of each Memcached entry. Defaults to 0.

Example:

ReactCC.renderToStaticMarkup(<App />, cache);

renderToNodeStream

  • component: The React component being rendered
  • cache: The component cache object
  • res: The response object that Express provides
  • htmlStart: Start of html markup in string form
  • htmlEnd: End of html markup in string form
  • memLife: (Only if using Memcached) An integer representing the lifetime (in seconds) of each Memcached entry. Defaults to 0.

Example:

let htmlStart = '<html><head><title>Page</title></head><body><div id="react-root">';
let htmlEnd =  '</div></body></html>';
ReactCC.renderToNodeStream(<App />, cache, res, htmlStart, htmlEnd);

renderToStaticNodeStream

  • component: The React component being rendered
  • cache: The component cache object
  • res: The response object that Express provides
  • htmlStart: Start of html markup in string form
  • htmlEnd: End of html markup in string form
  • memLife: (Only if using Memcached) An integer representing the lifetime (in seconds) of each Memcached entry. Defaults to 0.

Example:

let htmlStart = '<html><head><title>Page</title></head><body><div id="react-root">';
let htmlEnd = '</div></body></html>';
ReactCC.renderToStaticNodeStream(<App />, cache, res, htmlStart, htmlEnd);

Authors

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].