<input type="text" id="word" name="word" class="Stext" placeholder="输入药品的名称" />
<input name="" type="button" value="搜索" class="Sbutton" onclick="search(document.getElementById('word').value)" />
<script>
//候选字(输入框提示候选字)
$(".Stext").autocomplete({
minChars: 0, //自动完成激活之前填入的最小字符
width: 400, //提示的宽度,溢出隐藏
scrollHeight: 300, //提示的高度,溢出显示滚动条
matchContains: false, //包含匹配,就是data参数里的数据,是否只要包含文本框里的数据就显示
autoFill: true, //自动填充
source: function (request, response) {
$.ajax({
type:"post",
url: "/Public/CheckKey.ashx",
dataType: "json",
data: {
searchDbInforItem: $("#word").val()
},
success: function (data) {
var s = $.map(data, function (item) {
return {
value: item
}
});
response(s);
}
});
},
minLength: 1,
select: function (event, ui) {
$(".Stext").val(ui.value);
}
});
</script>
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string searchDbInforItem = context.Request.Form["searchDbInforItem"];
//string searchDbInforItem = "龙血竭胶囊";
List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();
using (var redisClient = PooledRedisHelper.GetClient())
{
if (redisClient.DbSize == 0)
{
RedisClient client = new RedisClient("127.0.0.1", 6379);
client.FlushAll();
//查出所有数据。
List<ProductInfo> list = Products.GetSearchKeyList();
for (int i = 0; i < list.Count; i++)
{
//逐字分割取出来的数据,插入到Redis缓存中
string[] keys = new string[list[i].prd_title.Length];
for (int j = 0; j < list[i].prd_title.Length; j++)
{
keys[j] = list[i].prd_title.Substring(j, 1);
client.AddItemToSortedSet(keys[j], list[i].prd_title);//这儿有个bug并没有存取所有的数据到Redis中。暂时没有找到原因
client.ExpireEntryAt(keys[j], DateTime.Now.AddDays(10));
}
client.AddItemToSortedSet(list[i].prd_title, list[i].prd_title);
client.ExpireEntryAt(list[i].prd_title, DateTime.Now.AddDays(10));
}
List<string> returnResult = redisClient.GetRangeFromSortedList(searchDbInforItem, 0, 20);
if (returnResult != null)
{
for (int i = 0; i < returnResult.Count; i++)
{
string title = returnResult[i];
result.Add(new KeyValuePair<string, string>(title, title));
}
List<string> returnList = new List<string>();
foreach (var item in result)
{
returnList.Add(item.Value);
}
JavaScriptSerializer json = new JavaScriptSerializer();
string returnJson = json.Serialize(returnList);
context.Response.Write(returnJson);
}
}
else
{
List<string> returnResult = redisClient.GetRangeFromSortedList(searchDbInforItem, 0, 20);
if (returnResult != null)
{
for (int i = 0; i < returnResult.Count; i++)
{
string title = returnResult[i];
result.Add(new KeyValuePair<string, string>(title, title));
}
List<string> returnList = new List<string>();
foreach (var item in result)
{
returnList.Add(item.Value);
}
JavaScriptSerializer json = new JavaScriptSerializer();
string returnJson = json.Serialize(returnList);
context.Response.Write(returnJson);
}
}
}
}
引用到了一个类
// 数据库
public static int InitialDb = 1;
private static PooledRedisClientManager instance = null;
public static PooledRedisClientManager Instance
{
get
{
if (instance == null)
{
instance = new PooledRedisClientManager(new string[] { "127.0.0.1:6379" });//注意这儿直接写IP和端口就行了。
}
return instance;
}
}
static PooledRedisHelper()
{
}
public static IRedisClient GetClient()
{
return Instance.GetClient();
}
}
引用到的Redis dll。
参考:
https://www.cnblogs.com/hnsongbiao/p/5102795.html
https://my.oschina.net/zimingforever/blog/63877/