1. jar パッケージをインポートします
2. 単純な条件付きクエリを実装します
ユーザー エンティティを作成するclass
public class User { private String id; private String name; private String sex; private int age; public String getId() { return id; } public User() { super(); } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public User(String id, String name, String sex, int age) { super(); this.id = id; this.name = name; this.sex = sex; this.age = age; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + "]"; } }
テストのために 5 つのオブジェクトを作成し、キャッシュに保存します
//连接redis Jedis jedis = new Jedis("127.0.0.1",6379); Map<String, String> map = new HashMap<String,String>(); final String USER_TABLE = "USER_TABLE"; //向缓存中存入5条数据组成的map String uuid1 = UUID.randomUUID().toString(); User user1 = new User(uuid1, "y1", "m", 15); //将对象转为json map.put(uuid1, JSONObject.fromObject(user1).toString()); String uuid2 = UUID.randomUUID().toString(); User user2 = new User(uuid2, "y2", "m", 18); map.put(uuid2, JSONObject.fromObject(user2).toString()); String uuid3 = UUID.randomUUID().toString(); User user3 = new User(uuid3, "y3", "n", 25); map.put(uuid3, JSONObject.fromObject(user3).toString()); String uuid4 = UUID.randomUUID().toString(); User user4 = new User(uuid4, "y4", "n", 15); map.put(uuid4, JSONObject.fromObject(user4).toString()); String uuid5 = UUID.randomUUID().toString(); User user5 = new User(uuid5, "y5", "m", 25); map.put(uuid5, JSONObject.fromObject(user5).toString()); //把map存到缓存中 jedis.hmset("USER_TABLE", map);
Redis でクエリを実行すると、5 つのユーザー オブジェクトがキャッシュに保存されていることがわかります
次に、最初に単一条件クエリを実装します。たとえば、年齢 15 歳、性別 m のユーザーをクエリします。
Redis は nosql なので、 mysql のように、条件付きクエリを実行するために使用される where を直接使用することはできません。そのため、Redis が条件付きクエリを実装したい場合は、条件を満たすすべてのユーザーをセットに保存するという愚かな方法しか使用できません。
Jedis jedis = new Jedis("127.0.0.1",6379); Map<String, String> map = new HashMap<String,String>(); final String USER_TABLE = "USER_TABLE"; //查询年龄为15,性别为n final String USER_TABLE_AGE_15 = "USER_TABLE_AGE_15"; final String USER_TABLE_SEX_m = "USER_TABLE_SEX_m"; final String USER_TABLE_SEX_n = "USER_TABLE_SEX_n"; //向缓存中存入5条数据组成的map String uuid1 = UUID.randomUUID().toString(); User user1 = new User(uuid1, "y1", "m", 15); //将对象转为json map.put(uuid1, JSONObject.fromObject(user1).toString()); //将符合条件的user的Id存到set中 jedis.sadd(USER_TABLE_AGE_15,uuid1); jedis.sadd(USER_TABLE_SEX_m,uuid1); String uuid2 = UUID.randomUUID().toString(); User user2 = new User(uuid2, "y2", "m", 18); map.put(uuid2, JSONObject.fromObject(user2).toString()); jedis.sadd(USER_TABLE_SEX_m,uuid2); String uuid3 = UUID.randomUUID().toString(); User user3 = new User(uuid3, "y3", "n", 25); map.put(uuid3, JSONObject.fromObject(user3).toString()); String uuid4 = UUID.randomUUID().toString(); User user4 = new User(uuid4, "y4", "n", 15); map.put(uuid4, JSONObject.fromObject(user4).toString()); jedis.sadd(USER_TABLE_AGE_15,uuid4); String uuid5 = UUID.randomUUID().toString(); User user5 = new User(uuid5, "y5", "m", 25); map.put(uuid5, JSONObject.fromObject(user5).toString()); jedis.sadd(USER_TABLE_SEX_m,uuid5); //把map存到缓存中 jedis.hmset("USER_TABLE", map);
したがって、年齢 15 歳のユーザーをクエリする場合は、まず USER_TABLE_AGE_15 からすべての uuid を削除してから、USER_TABLE_
からユーザーを削除する必要があります。//获取年龄为15的user的uuid Set<String> age = jedis.smembers(USER_TABLE_AGE_15); //根据uuid获取user List<User> userJson = new ArrayList<User>(); for (Iterator iterator = age.iterator(); iterator.hasNext();) { String string = (String) iterator.next(); String jsonStr = jedis.hget(USER_TABLE, string); JSONObject json = JSONObject.fromObject(jsonStr); User user = (User) JSONObject.toBean(json, User.class); userJson.add(user); System.out.println(user); }
結果は次のとおりです:
User [id=63a970ec-e997-43e0-8ed9-14c5eb87de8b, name=y1, sex=m, age=15] User [id=aa074a2a-88d9-4b50-a99f-1375539164f7, name=y4, sex=n, age=15]
したがって、年齢 15 で性別 m のユーザーが必要な場合は、非常に簡単です。
USER_TABLE_AGE_15 と USER_TABLE_SEX_m の和集合を取得し、それを取得します。 from USER_TABLE.
//获取年龄为15并性别为m的user Set<String> userSet = jedis.sinter(USER_TABLE_AGE_15,USER_TABLE_SEX_m); List<User> users = new ArrayList<User>(); for (Iterator iterator = userSet.iterator(); iterator.hasNext();) { String string = (String) iterator.next(); String jsonStr = jedis.hget(USER_TABLE, string); JSONObject json = JSONObject.fromObject(jsonStr); User user = (User) JSONObject.toBean(json, User.class); users.add(user); System.out.println(user); }
User [id=63a970ec-e997-43e0-8ed9-14c5eb87de8b, name=y1, sex=m, age=15]
Redis の詳細については、redis 入門チュートリアル 列に注目してください。
以上がRedis は単純な条件付きクエリを実装しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

redisactsassassadatastoreandaservice.1)asadatastore、itusesin memorystorage for fastorations、supporting variousdatastructureSlike-key-valuepairsandsortedsets.2)asaservice、iteasruascruascriptingrupting criptingforceptingpurplecomplecomplecprexoperations

他のデータベースと比較して、Redisには次の独自の利点があります。1)非常に速い速度、および読み取り操作は通常、マイクロ秒レベルにあります。 2)豊富なデータ構造と操作をサポートします。 3)キャッシュ、カウンター、公開サブスクリプションなどの柔軟な使用シナリオ。 Redisまたはその他のデータベースを選択する場合、特定のニーズとシナリオに依存します。 Redisは、高性能および低遅延のアプリケーションでうまく機能します。

Redisは、データストレージと管理において重要な役割を果たしており、複数のデータ構造と持続性メカニズムを通じて最新のアプリケーションの中核となっています。 1)Redisは、文字列、リスト、コレクション、注文されたコレクション、ハッシュテーブルなどのデータ構造をサポートし、キャッシュや複雑なビジネスロジックに適しています。 2)RDBとAOFの2つの持続方法を通じて、Redisは信頼できるストレージとデータの迅速な回復を保証します。

Redisは、大規模なデータの効率的なストレージとアクセスに適したNOSQLデータベースです。 1.Redisは、複数のデータ構造をサポートするオープンソースメモリデータ構造ストレージシステムです。 2.キャッシュ、セッション管理などに適した、非常に速い読み取り速度と書き込み速度を提供します。 4.使用例には、基本的なキー値ペア操作と高度なコレクション重複排除関数が含まれます。 5.一般的なエラーには、接続の問題、データ型の不一致、メモリオーバーフローが含まれるため、デバッグに注意する必要があります。 6.パフォーマンス最適化の提案には、適切なデータ構造の選択とメモリ排除戦略の設定が含まれます。

現実世界でのRedisのアプリケーションには、1。キャッシュシステムとして、データベースクエリを加速し、2。Webアプリケーションのセッションデータを保存するには、3。リアルタイムランキングを実装する4。メッセージ配信をメッセージキューとして簡素化する。 Redisの汎用性と高性能により、これらのシナリオで輝きます。

Redisは、高速、汎用性、豊富なデータ構造のために際立っています。 1)Redisは、文字列、リスト、コレクション、ハッシュなどのデータ構造をサポートし、コレクションを注文します。 2)メモリを介してデータを保存し、RDBとAOFの持続性をサポートします。 3)Redis 6.0から始めて、マルチスレッドI/O操作が導入されました。これにより、高い並行性シナリオでパフォーマンスが向上しました。

redisisclassifiedsaNosqldatabasebasesakey-valuedataModelinsteaded ofthetraditionaldatabasemodel.itoffersspeedand andffficability、makingidealforreal-timeaplications andcaching、butmaynotbesbesutable fors cenariois requiring datientiantientioniity

Redisは、データをキャッシュし、分散ロックとデータの持続性を実装することにより、アプリケーションのパフォーマンスとスケーラビリティを向上させます。 1)キャッシュデータ:Redisを使用して頻繁にアクセスしたデータをキャッシュして、データアクセス速度を向上させます。 2)分散ロック:Redisを使用して分散ロックを実装して、分散環境での操作のセキュリティを確保します。 3)データの持続性:データの損失を防ぐために、RDBおよびAOFメカニズムを介してデータセキュリティを確保します。


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 中国語版
中国語版、とても使いやすい

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

WebStorm Mac版
便利なJavaScript開発ツール

mPDF
mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。
