Sfoglia il codice sorgente

palicanon 用redis

visuddhinanda 5 anni fa
parent
commit
9753d0eccf

+ 23 - 5
app/palicanon/get_chapter_children.php

@@ -1,6 +1,9 @@
 <?php
 require_once '../path.php';
 
+$redis = new redis();  
+$r_conn = $redis->connect('127.0.0.1', 6379);
+
 $dns = "sqlite:"._FILE_DB_PALI_TOC_;
 $dbh_toc = new PDO($dns, "", "",array(PDO::ATTR_PERSISTENT=>true));
 $dbh_toc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);  
@@ -36,11 +39,26 @@ if($paraInfo){
 		$paraList = $stmt->fetchAll(PDO::FETCH_ASSOC);	
 		foreach ($paraList as $key => $value) {
 			# 查进度
-			$query = "SELECT lang, all_trans from progress_chapter where book=? and para=?";
-			$stmt = $dbh_toc->prepare($query);
-			$sth_toc = $dbh_toc->prepare($query);
-			$sth_toc->execute(array($value["book"],$value["para"]));
-			$paraProgress = $sth_toc->fetch(PDO::FETCH_ASSOC);
+			$paraProgress = false;
+			if($r_conn){
+				$count = $redis->hLen("progress_chapter_{$value["book"]}_{$value["para"]}");
+				if($count>0){
+					$prog = $redis->hGetAll("progress_chapter_{$value["book"]}_{$value["para"]}");
+					foreach ($prog as $keylang => $valuetrans) {
+						# code...
+						$paraProgress = array("lang"=>$keylang,"all_trans"=>$valuetrans);
+						break;
+					}
+				}
+			}
+			else{
+				$query = "SELECT lang, all_trans from progress_chapter where book=? and para=?";
+				$stmt = $dbh_toc->prepare($query);
+				$sth_toc = $dbh_toc->prepare($query);
+				$sth_toc->execute(array($value["book"],$value["para"]));
+				$paraProgress = $sth_toc->fetch(PDO::FETCH_ASSOC);				
+			}
+
 			$paraList[$key]["progress"]=$paraProgress;
 
 			#查标题

+ 46 - 0
app/redis/upgrade_pali_sent.php

@@ -0,0 +1,46 @@
+<?php
+/*
+get user sentence from db
+*/
+require_once "../path.php";
+require_once "../public/_pdo.php";
+require_once "../public/function.php";
+
+if(isset($argv[1])){
+	if($argv[1]=="del"){
+		$redis = new redis();  
+		$r_conn = $redis->connect('127.0.0.1', 6379);  
+		$keys = 'pali_sent_*';
+		$redis->delete($redis->keys($keys));
+	}
+}
+else{
+	$dns = "sqlite:"._FILE_DB_PALI_SENTENCE_;
+	$dbh = new PDO($dns, "", "",array(PDO::ATTR_PERSISTENT=>true));
+	$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);  
+
+	$query="SELECT book,paragraph, begin,end ,text FROM pali_sent WHERE 1 ";
+	$stmt = $dbh->prepare($query);
+	$stmt->execute();
+	$redis = new redis();  
+	$r_conn = $redis->connect('127.0.0.1', 6379);  
+	$stringSize = 0;
+	if($r_conn){
+		while($sent = $stmt->fetch(PDO::FETCH_ASSOC)){
+			$stringSize += strlen($sent["text"]);
+			if($stringSize>50000000){
+				sleep(1);
+				$stringSize=0;
+				echo $sent["book"]."_".$sent["paragraph"]."\n";
+			}
+			$result = $redis->set('pali_sent_'.$sent["book"]."_".$sent["paragraph"]."_".$sent["begin"]."_".$sent["end"],$sent["text"]);  
+		}
+		echo "完成";
+	}
+	else{
+		echo "连接redis失败";
+	}
+}
+
+
+?>

+ 0 - 37
app/redis/upgred_pali_sent.php

@@ -1,37 +0,0 @@
-<?php
-/*
-get user sentence from db
-*/
-require_once "../path.php";
-require_once "../public/_pdo.php";
-require_once "../public/function.php";
-
-
-
-$dns = "sqlite:"._FILE_DB_PALI_SENTENCE_;
-$dbh = new PDO($dns, "", "",array(PDO::ATTR_PERSISTENT=>true));
-$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);  
-
-$query="SELECT book,paragraph, begin,end ,text FROM pali_sent WHERE 1 ";
-$stmt = $dbh->prepare($query);
-$stmt->execute();
-$redis = new redis();  
-$r_conn = $redis->connect('127.0.0.1', 6379);  
-$stringSize = 0;
-if($r_conn){
-	while($sent = $stmt->fetch(PDO::FETCH_ASSOC)){
-		$stringSize += strlen($sent["text"]);
-		if($stringSize>50000000){
-			sleep(1);
-			$stringSize=0;
-			echo $sent["book"]."_".$sent["paragraph"]."\n";
-		}
-		$result = $redis->set('pali_sent_'.$sent["book"]."_".$sent["paragraph"]."_".$sent["begin"]."_".$sent["end"],$sent["text"]);  
-	}
-	echo "完成";
-}
-else{
-	echo "连接redis失败";
-}
-
-?>

+ 34 - 11
app/upgrade/upgrade_pali_toc.php

@@ -2,6 +2,8 @@
 #升级段落完成度数据库
 require_once '../path.php';
 
+$redis = new redis();  
+$r_conn = $redis->connect('127.0.0.1', 6379);
 
 $dns = "sqlite:"._FILE_DB_PALI_TOC_;
 $dbh_toc = new PDO($dns, "", "",array(PDO::ATTR_PERSISTENT=>true));
@@ -84,7 +86,12 @@ foreach ($result_lang as $lang) {
 			else{
 				$para_strlen = 0;
 			}
-			$sth_toc->execute(array($para["book"],$para["paragraph"],$lang["language"],$para_strlen,0));
+			if($r_conn){
+				$redis->hSet("progress_{$para["book"]}-{$para["paragraph"]}", $lang["language"], $para_strlen);
+			}
+			else{
+				$sth_toc->execute(array($para["book"],$para["paragraph"],$lang["language"],$para_strlen,0));
+			}
 		}
 	}
 }
@@ -119,21 +126,37 @@ foreach ($valid_book as $key => $book) {
 			$pali_strlen = (int)$result_chapter_strlen["pali_strlen"];
 			# 译文等效字符数
 			foreach ($result_lang as $lang) {
-				$query = "SELECT sum(all_strlen) as all_strlen from progress where book = ? and (para between ? and ? )and lang = ?";
-				$stmt = $dbh_toc->prepare($query);
-				$stmt->execute(array($book["book"],$chapter["paragraph"],(int)$chapter["paragraph"]+(int)$chapter["chapter_len"]-1,$lang["language"]));
-				$result_chapter_trans_strlen = $stmt->fetch(PDO::FETCH_ASSOC);
-				if($result_chapter_trans_strlen){
-					$tran_strlen = (int)$result_chapter_trans_strlen["all_strlen"];
+
+				if($r_conn){
+					$tran_strlen=0;
+					for ($i=$chapter["paragraph"]; $i <(int)$chapter["paragraph"]+(int)$chapter["chapter_len"] ; $i++) { 
+						# code...
+						$all_strlen = $redis->hGet("progress_{$book["book"]}-{$i}", $lang["language"]);
+						if($all_strlen){
+							$tran_strlen+=$all_strlen;
+						}
+					}
 					if($tran_strlen>0){
 						$progress = $tran_strlen/$pali_strlen;
-						$sth_toc->execute(array($book["book"],$chapter["paragraph"],$lang["language"],$progress,0));
-						//echo "{$book["book"]},{$chapter["paragraph"]},{$lang["language"]},{$progress}\n";
+						$redis->hSet("progress_chapter_{$book["book"]}_{$chapter["paragraph"]}", $lang["language"], $progress);
+					}
+				}
+				else
+				{
+					$query = "SELECT sum(all_strlen) as all_strlen from progress where book = ? and (para between ? and ? )and lang = ?";
+					$stmt = $dbh_toc->prepare($query);
+					$stmt->execute(array($book["book"],$chapter["paragraph"],(int)$chapter["paragraph"]+(int)$chapter["chapter_len"]-1,$lang["language"]));
+					$result_chapter_trans_strlen = $stmt->fetch(PDO::FETCH_ASSOC);
+					if($result_chapter_trans_strlen){
+						$tran_strlen = (int)$result_chapter_trans_strlen["all_strlen"];
+						if($tran_strlen>0){
+							$progress = $tran_strlen/$pali_strlen;
+							$sth_toc->execute(array($book["book"],$chapter["paragraph"],$lang["language"],$progress,0));
+						}
 					}
-
 				}
 				#插入段落数据
-			}			
+			}
 		}
 	}
 }