暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 通过ORDS缓存PLSQL包

ASKTOM 2019-03-05
396

问题描述

当通过rest服务多次调用plsql过程时,我需要缓存很少的值,即当它从同一用户多次执行以进行优化时。

我正在通过ORDS rest service致电以下包裹程序。

下面是包代码。

create or replace
PACKAGE rest_api_cache
as
g_val number;
procedure g_proc (p_val in number,
                  p_found in out varchar2);
END rest_api_cache;


create or replace
PACKAGE BODY rest_api_cache
as
procedure g_proc (p_val in number,
                  p_found in out varchar2)
                  
is
l number;
begin
if rest_api_cache.g_val is null then
   rest_api_cache.g_val  := p_Val;
   p_found := 'FALSE';
else
  p_found := 'TRUE';
end if;
end g_proc;
END rest_api_cache;

---------------------------------
我已经使用下面的脚本将包创建为ORDS plsql过程

BEGIN
  ORDS.define_module(
    p_module_name    => 'testmodule6a',
    p_base_path      => 'testmodule6a/',
    p_items_per_page => 0);
  
  ORDS.define_template(
   p_module_name    => 'testmodule6a',
   p_pattern        => 'restcache');

  ORDS.define_handler(
    p_module_name    => 'testmodule6a',
    p_pattern        => 'restcache',
    p_method         => 'POST',
    p_source_type    => ORDS.source_type_plsql,
    p_source         => 'BEGIN
                           rest_api_cache.g_proc(
                             p_val      => :val,
                             p_found    => :found
                             );
                         END;',
    p_items_per_page => 0);

  ORDS.define_parameter(
    p_module_name        => 'testmodule6a',
    p_pattern            => 'restcache',
    p_method             => 'POST',
    p_name               => 'cachefound',
    p_bind_variable_name => 'found',
    p_source_type        => 'RESPONSE',
    p_param_type         => 'STRING',
    p_access_method      => 'OUT'
  );
  
  
  ORDS.define_parameter(
    p_module_name        => 'testmodule6a',
    p_pattern            => 'restcache',
    p_method             => 'POST',
    p_name               => 'p_value',
    p_bind_variable_name => 'val',
    p_source_type        => 'RESPONSE',
    p_param_type         => 'INT',
    p_access_method      => 'IN'
  );
  
  
  COMMIT;
END;
/


----------------------------------------------------
我正在通过rest服务执行程序

使用以下输入参数创建有效负载文件out-param-payload.json以调用过程
{"val": 7499}

使用以下命令执行过程

curl -i -X POST --data-binary @out-param-payload.json -H "Content-Type: application/json" http://localhost:8080/ords/testuser1/testmodule6a/restcache


上述调用的输出为

HTTP/1.1 200 OK
Date: Tue, 05 Mar 2019 03:13:21 GMT
Content-Type: application/json
Transfer-Encoding: chunked

{"cachefound":"FALSE"}



对于来自同一用户的立即执行,该值返回为 “FALSE”,即包状态被清除,有没有一种方法,我可以存储包变量为同一用户重复执行。

专家解答

REST是一个无状态协议。所以每次对ORDS的调用都会重置包状态。

你如何解决这个问题取决于你想做什么。您可以使用应用程序上下文或将数据存储在表中。

或者创建一个API,它在一个调用中完成所有工作。

所以问题是: 为什么要跨呼叫保持状态?
文章转载自ASKTOM,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论