npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-smart-cache

v0.0.3

Published

auto put data in cache by state key, and you can manage cache duetime by config

Downloads

1

Readme

安装:

npm install --save redux-smart-cache

使用

1.配置持久化的实现,以及缓存的时间

例如:在react-native app里,我们可以这样做

import { cacheConfig } from 'redux-smart-cache'
import { AsyncStorage } from 'react-native'
// 实现了缓存的存储,返回Promise,result是void
cacheConfig.save = async function(key, value){
  const data = JSON.stringify(value)
  await AsyncStorage.setItem(key, data)
}
// 实现缓存的读取,返回Pormise,result是一个json对象
cacheConfig.load = async function(key){
  const value =  await AsyncStorage.getItem(key)
  return JSON.parse(value)
}
// 设置userinfo这个state的缓存时间为1小时
cacheConfig.cacheDueTime = {
  userinfo: 3600 * 1000
}
// 设置默认的缓存时间为200秒
cacheConfig.defaultDueTime = 200 * 1000

2. Action

function loadUserinfo(userId) {
  return {
    // 配置尝试,成功,失败三种type
    types: [
      'USERINFO_LOAD_ATTEMPT',
      'USERINFO_LOAD_SUCCESS',
      'USERINFO_LOAD_FAILURE'
    ],
    // 实现callAPI接口,是个promise接口
    callAPI: async () => {
      const res = await fetch(`http://example.com/users/${userId}`)
      return {
        userinfo: res.body.userinfo,  
        //结果是否有效,该属性为true,则使用cache,否则不做任何处理
        effective: res.body.errorNo == 0
      }
    },
    // 第一个type的action载体
    payload: {userId, attempting: true},
    // 是否强制更新,如果为false,则使用cache
    forceUpdate: false
  }
}  

3.配置reducer, combineReducer, 及使用中间件

import {cacheMiddleWare} from 'react-smart-cache'
function userinfoReducer(state = {attempting: false}, action) {
  switch (action.type) {
  case 'USERINFO_LOAD_ATTEMPT':
    return state.merge({ attempting: true })
  case 'USERINFO_LOAD_SUCCESS':
    return state.merge({ attempting: false, errorCode: null, userinfo: action.userinfo})
  case 'USERINFO_LOAD_FAILURE':
    return state.merge({ attempting: false, errorCode: action.errorCode })
  default:
    return state
  }
}
const rootReducer = combineReducers({
  userinfo: userinfoReducer
})
const enhancers = compose(
  applyMiddleware(cacheMiddleWare)
)

const store = createStore(
  rootReducer, 
  enhancers
)

依赖

redux

es-2015

es6