【问题标题】:ReactJS and Spring Data Rest caching issue possibly related to websocketsReactJS 和 Spring Data Rest 缓存问题可能与 websockets 相关
【发布时间】:2016-05-18 22:53:18
【问题描述】:

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

我最近炸毁了我的数据库。在我开始在我的系统中创建一些新的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)。 此时它返回旧的用户详细信息而不是新的详细信息

  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 也不能解决问题。读后感觉这是缓存组件而不是数据。

谢谢

【问题讨论】:

    标签: javascript node.js spring reactjs spring-data-rest


    【解决方案1】:

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

    'Pragma': 'no-cache', 
    
    'Expires': '-1', 
    
    'cache-control': 'no-cache'
    

    【讨论】:

      猜你喜欢
      • 2015-05-25
      • 2013-08-24
      • 2017-02-11
      • 2016-02-01
      • 2016-10-26
      • 2014-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多