Skip to content
Advertisement

Somehow my Fask API returns me old values

In debug:

I run ng serve on my angular project, make the request and receive the updated values

In production

I update de files of the API and the angular project, make the request and receive old data. If I open the API link in a new tab I get the updated values, even the files in the server are acting like that but if I open their links, even when deleted, I still can access it.

Even when I deleted all the values in a table I still get outdated data

Both production and debug access the same database

Technologies:

  • Front-end: Angular
  • Back-end: Flask

What I’ve tried:

  • Set @@AUTOCOMMIT to 1
  • Change cache in order to not be saved

Here is the code of one endpoint:

    @app.route('/api/produtos/categoria/<categoria>', methods=['GET'])
    @cross_origin(supports_credentials=True)
    def getProdutosByCategoria(categoria):
        print(request.headers)
        if request.method == 'GET':
            sql = "SELECT p.id, p.data_cadastro, p.nome, p.certificado_aprovacao, p.data_validade, p.fabricante, p.complemento, p.tamanho, p.tipo, p.preco, p.quantidade_estoque, cp.nome, p.link_imagem FROM produtos p JOIN categoria_produtos cp ON p.categoria_id = cp.id WHERE a_venda = 1 AND categoria_id = %s" % (categoria)
    
            try:
                mydb.commit()
                cursor.execute(sql)
                data = cursor.fetchall()
                result = []
                for item in data:
                    result.append({
                        "Id": item[0],
                        "DataCadastro": item[1],
                        "Nome": item[2],
                        "CertificadoAprovação": item[3],
                        "DataValidade": item[4],
                        "Fabricante": item[5],
                        "Complemento": item[6],
                        "Tamanho": item[7],
                        "Tipo": item[8],
                        "Preco": item[9],
                        "QuantidadeEstoque": item[10],
                        "Categoria": item[11],
                        'Imagem': item[12]
                    }) 
            except:
                return {'Status': 'erro', "Message": "Não foi possível carregar os produtos"}
            else:
                return {'Status': 'success', "Message": "Produtos carregados com sucesso", "Data": result}

Caching system

this is added after every request

@app.after_request
def after_request(response):
    response.headers.add('Access-Control-Allow-Origin', 'http://censored-domain/')
    response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
    response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
    response.headers.add('Access-Control-Allow-Credentials', 'true')
    response.cache_control.max_age = 60
    if 'Cache-Control' not in response.headers:
        response.headers['Cache-Control'] = 'no-store'
    return response

Advertisement

Answer

Solved by removing cache

My thought:

Since in development I’ll have to make the same request many times, I removed cache from the Angular project and will only put it back when it is in production.

Thanks for the help

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement