Skip to content

Commit 340816c

Browse files
committed
Return prepare query cache in sdk
1 parent 184888f commit 340816c

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

src/Session.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,11 +416,18 @@ public function prepare($yql)
416416
{
417417
$statement = new Statement($this, $yql);
418418

419+
if ($statement->isCached())
420+
{
421+
return $statement;
422+
}
423+
419424
$result = $this->request('PrepareDataQuery', [
420425
'session_id' => $this->session_id,
421426
'yql_text' => $yql,
422427
]);
423428

429+
$statement->saveInCache();
430+
424431
return $statement;
425432
}
426433

src/Statement.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,26 @@ class Statement
2121
*/
2222
protected $yql;
2323

24+
/**
25+
* @var string
26+
*/
27+
protected $qhash;
28+
29+
/**
30+
* @var bool
31+
*/
32+
protected $cached = false;
33+
2434
/**
2535
* @var array
2636
*/
2737
protected $params = [];
2838

39+
/**
40+
* @var array
41+
*/
42+
protected static $qcache = [];
43+
2944
/**
3045
* @param Session $session
3146
* @param string $yql
@@ -35,6 +50,7 @@ public function __construct(Session $session, $yql)
3550
$this->session = $session;
3651
$this->yql = $yql;
3752

53+
$this->checkQueryCache();
3854
$this->detectParams();
3955
}
4056

@@ -52,6 +68,14 @@ public function execute(array $parameters = [])
5268
return $this->session->query($q, $this->prepareParameters($parameters));
5369
}
5470

71+
/**
72+
* @return bool
73+
*/
74+
public function isCached()
75+
{
76+
return $this->cached;
77+
}
78+
5579
/**
5680
* @param array $parameters
5781
* @return array
@@ -81,6 +105,18 @@ protected function prepareParameters($parameters)
81105
return $data;
82106
}
83107

108+
/**
109+
* @return void
110+
*/
111+
protected function checkQueryCache()
112+
{
113+
$this->qhash = sha1(trim($this->yql));
114+
if (in_array($this->qhash, static::$qcache))
115+
{
116+
$this->cached = true;
117+
}
118+
}
119+
84120
/**
85121
* @return void
86122
*/
@@ -95,4 +131,8 @@ protected function detectParams()
95131
}
96132
}
97133
}
134+
135+
public function saveInCache(){
136+
static::$qcache[$this->qhash] = $this->qhash;
137+
}
98138
}

tests/PreparedQueryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function test(){
3131

3232
$session = $table->createSession();
3333

34-
$prepared_query = $ydb->table()->session()->prepare('
34+
$prepared_query = $session->prepare('
3535
declare $pk as Int64;
3636
select $pk;');
3737
$x = 2;

0 commit comments

Comments
 (0)