HandlerSocket 是一个mysql 数据库的插件,它可以绕过mysql的查询分析和优化的过程,直接与innodb存储引擎进行交互。尤其是当大多数数据都被innodb缓存到内存中的时候,
查询分析和优化过程就会是整个查询处理过程的瓶颈。通过使用HandlerSocket可以绕过这个瓶颈,从而提升性能。这要比mysql+memched要有优势,因为HandlerSocket不需要处理缓存失效的问题。下面看下HsClient访问mysql的一个例子代码
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using HsClient;
6
7 namespace HsClientTest
8 {
9 class Program
10 {
11 private static HsIndexSessionFactory client = new HsIndexSessionFactory("192.168.200.9", 9999);
12 static void Main(string[] args)
13 {
14 HsIndexSession session = null;
15 try
16 {
17 session = client.OpenIndex(0, "test", "FUser", "PRIMARY",
18 new string[] { "UserId", "Nickname", "Email" });
19
20 HsResultSet rs = session.Find(HsOperator.GreatEqual, new string[] { "0" }, 20, 0);
21
22 while (rs.Next())
23 {
24 Console.WriteLine("id:{0},nickname:{1},email:{2}", rs.GetInt(0), rs.GetString(1), rs.GetString(2));
25 }
26
27 int count = session.Update(HsOperator.Equal, new string[] { "6" }, 1, 0, new string[] { "6", "c#", "x@x.net" });
28 Console.WriteLine("update count {0}", count);
29
30
31 count = session.Delete(HsOperator.Equal, new string[] { "6" }, 1, 0);
32 Console.WriteLine("delete count {0}", count);
33
34
35 bool insertResult = session.Insert(new string[] { "6", "haha", "you@aa.ss" });
36 Console.WriteLine("insert success {0}", insertResult);
37 }
38 finally
39 {
40 if (session != null)
41 {
42 session.Close();
43 }
44 }
45
46
47 //close all socket in the end
48 client.Shutdown();
49 Console.ReadKey();
50 }
51 }
52 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using HsClient;
6
7 namespace HsClientTest
8 {
9 class Program
10 {
11 private static HsIndexSessionFactory client = new HsIndexSessionFactory("192.168.200.9", 9999);
12 static void Main(string[] args)
13 {
14 HsIndexSession session = null;
15 try
16 {
17 session = client.OpenIndex(0, "test", "FUser", "PRIMARY",
18 new string[] { "UserId", "Nickname", "Email" });
19
20 HsResultSet rs = session.Find(HsOperator.GreatEqual, new string[] { "0" }, 20, 0);
21
22 while (rs.Next())
23 {
24 Console.WriteLine("id:{0},nickname:{1},email:{2}", rs.GetInt(0), rs.GetString(1), rs.GetString(2));
25 }
26
27 int count = session.Update(HsOperator.Equal, new string[] { "6" }, 1, 0, new string[] { "6", "c#", "x@x.net" });
28 Console.WriteLine("update count {0}", count);
29
30
31 count = session.Delete(HsOperator.Equal, new string[] { "6" }, 1, 0);
32 Console.WriteLine("delete count {0}", count);
33
34
35 bool insertResult = session.Insert(new string[] { "6", "haha", "you@aa.ss" });
36 Console.WriteLine("insert success {0}", insertResult);
37 }
38 finally
39 {
40 if (session != null)
41 {
42 session.Close();
43 }
44 }
45
46
47 //close all socket in the end
48 client.Shutdown();
49 Console.ReadKey();
50 }
51 }
52 }
要访问mysql必须首先打开一个IndexSession.指明我们要访问的索引名称和要访问的字段。很容易理解,因为没有了mysql原来的查询分析和优化。所以访问表数据使用那个索引
就必须我们自己来选择。 注意通过IndexSession发起的CRUD操作的字段数量很顺序必须和打开IndexSession时提供的字段是一样的。使用完IndexSession要记得关闭,关闭会吧IndexSession使用的Socket连接,交还给连接池。最后Shutdown操作会关闭连接池中所有连接.
下载/Files/xhan/HsClient.rar 源代码也就500行左右,学习网络编程的同学可以看看。
代码还没经过生产环境测试,慎重使用,仅供学习交流