ReactJS 和 Spring Data Rest 缓存问题可能与 websockets 有关

ReactJS and Spring Data Rest caching issue possibly related to websockets

我似乎遇到了一个奇怪的缓存问题。

我最近炸掉了我的数据库。在我开始在我的系统中创建一些新的 users 之后,我注意到出现了一个老用户。该用户尚未重新创建,目前不存在于数据库中。

我不确定从 Spring Data RESTReactJS 哪里寻找缓存问题。有谁知道这可能来自哪里?

我也有 web sockets 设置。

步骤:

吹走分贝: 创建了一个新用户 Web 套接字进行创建 然后,我使用 reactjs 进行调用以获取来自 Spring Data Rest 的用户页面,这就是奇怪发生的地方。

调用获取用户

  Headers:

  Request URL:http://localhost:8081/api/users?size=4
  Request Method:GET
  Status Code:200 OK
  Remote Address:[::1]:8081

  Response Headers
  view source
  Content-Type:application/hal+json;charset=UTF-8
  Date:Wed, 18 May 2016 22:25:29 GMT
  Server:Apache-Coyote/1.1
  Transfer-Encoding:chunked

  Request Headers
  view source
  Accept:application/hal+json
  Accept-Encoding:gzip, deflate, sdch
  Accept-Language:en-US,en;q=0.8
  Connection:keep-alive
  Content-Type:text/plain
  Host:localhost:8081
  Referer:http://localhost:8081/
  User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
  Query String Parameters
  view source
  view URL encoded
  size:4

  BODY:

  {
    "_embedded" : {
      "users" : [ {
        "firstName" : "Aaron",
        "lastName" : "Magi",
        "userName" : "amagi",
        "description" : "coding up a storm",
        "_links" : {
          "self" : {
            "href" : "http://localhost:8081/api/users/1"
          },
          "user" : {
            "href" : "http://localhost:8081/api/users/1"
          }
        }
      }, {
        "firstName" : "john",
        "lastName" : "smith",
        "userName" : "jsmith",
        "description" : "mr smith",
        "_links" : {
          "self" : {
            "href" : "http://localhost:8081/api/users/3"
          },
          "user" : {
            "href" : "http://localhost:8081/api/users/3"
          }
        }
      } ]
    },
    "_links" : {
      "self" : {
        "href" : "http://localhost:8081/api/users"
      },
      "profile" : {
        "href" : "http://localhost:8081/api/profile/users"
      }
    },
    "page" : {
      "size" : 4,
      "totalElements" : 2,
      "totalPages" : 1,
      "number" : 0
    }
  }

然后我在内部调用端点以获取用户详细信息 (http://localhost:8081/api/users/3)。 此时 returns 旧用户详细信息不是新详细信息

  Headers:

  Request URL:http://localhost:8081/api/users/3
  Request Method:GET
  Status Code:304 Not Modified
  Remote Address:[::1]:8081

  Response Headers
  view source
  Date:Wed, 18 May 2016 22:25:29 GMT
  ETag:"0"
  Server:Apache-Coyote/1.1

  Request Headers
  view source
  Accept:application/hal+json
  Accept-Encoding:gzip, deflate, sdch
  Accept-Language:en-US,en;q=0.8
  Connection:keep-alive
  Content-Type:text/plain
  Host:localhost:8081
  If-None-Match:"0"
  Referer:http://localhost:8081/
  User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36


  BODY

      {
        "firstName" : "Stephen",
        "lastName" : "Mann",
        "userName" : "smann",
        "description" : "Wizard",
        "_links" : {
          "self" : {
            "href" : "http://localhost:8081/api/users/3"
          },
          "user" : {
            "href" : "http://localhost:8081/api/users/3"
          }
        }
      }

webpack.config

  var path = require('path');

var node_dir = __dirname + '/node_modules';

module.exports = {
    entry: './app.js',
    devtool: 'sourcemaps',
    cache: false,
    debug: true,
    resolve: {
        alias: {
            'stompjs': node_dir + '/stompjs/lib/stomp.js',
        }
    },
    output: {
        path: __dirname,
        filename: './built/bundle.js'
    },
    module: {
        loaders: [
            {
                test: path.join(__dirname, '.'),
                exclude: /(node_modules)/,
                loader: 'babel-loader'
                // TODO remove for production
                //loaders: ['react-hot','babel-loader']
            }
        ]
    }
};

Application.properties

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

# Web server
spring.data.rest.base-path=/api
server.port=8081

# TODO FOR DEBUG ONLY
spring.thymeleaf.cache=false
spring.freemarker.cache=false
spring.groovy.template.cache=false
spring.velocity.cache=false
spring.mustache.cache=false
server.session.persistent=true
spring.h2.console.enabled=true
spring.resources.cache-period=0

问题

缓存发生在哪里?我可以继续创建用户并让他们使用旧数据不正确地显示。有没有办法为您的应用程序正确关闭此功能?

我在发布这个问题时注意到我在 webpack.config.js 中有 cache:true。但即使将其设置为 false 也没有解决问题。看完后听起来这是缓存组件而不是数据。

谢谢

原来缓存是一个IE相关的问题。进一步阅读后,我确定我需要设置缓存控制

'Pragma': 'no-cache', 

'Expires': '-1', 

'cache-control': 'no-cache'