ReactJS和SpringDataRest缓存问题可能与websocket有关

ReactJS and Spring Data Rest caching issue possibly related to websockets

本文关键字:websocket 有关 问题 SpringDataRest 缓存 ReactJS      更新时间:2023-09-26

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

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

我不确定从哪里查找缓存问题,无论是从Spring Data REST还是从ReactJS。有人知道这可能是从哪里来的吗?

我也有web sockets设置。

步骤:

吹扫db:创建了新用户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)此时它返回旧的用户详细信息,而不是新的详细信息

  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'