从Redis hash获得最佳结果

时间:2017-06-12 01:18:32

标签: redis

我正在尝试在Redis中编写查询以获取我的哈希键的前2个字段值。

基本上,当我HVALS hashname时,我想获得添加的前2个字段的值(最旧的2个)。这有点像在SQL数据库中获取TOP 2元组。

这是否可以在redis中使用?

2 个答案:

答案 0 :(得分:3)

不,这是不可能的。 Redis Hash中的字段和值的顺序是随机的(无论是通过对小哈希进行实验获得的经验证据)所有意图和目的。有关订购元素,请参阅Redis'排序集。

更新:为了回答评论中的问题,IIUC看起来你可以用Strings轻松解决它。因为Redis'自然,在任何给定时刻,有一个用户等待特定匹配,或者为零。如果密钥不存在(即等待匹配的零用户),则SET matchmaking:blue username1:token可以GET,如果密钥存在,则DELSET。请务必使用MULTI/EXEC" NX"子命令,@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); //Get the list of shoes new getSneakers().execute(); mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawerToggle = setupDrawerToggle(); navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); // Tie DrawerLayout events to the ActionBarToggle mDrawer.addDrawerListener(drawerToggle); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. drawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Pass any configuration change to the drawer toggles drawerToggle.onConfigurationChanged(newConfig); } private ActionBarDrawerToggle setupDrawerToggle() { return new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.drawer_close); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (drawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } //@SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); if (id == R.id.all_sneak) { LinearLayout mainLayout = (LinearLayout) findViewById(R.id. main_container); LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.content_main, null); mainLayout.removeAllViews(); mainLayout.addView(layout); new getSneakers().execute(); } else if (id == R.id.my_colleciton) { } else if (id == R.id.nav_want) { } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawer.closeDrawer(GravityCompat.START); return true; } 和/或Lua,以确保这两个逻辑操作的原子性。

答案 1 :(得分:0)

根据我的实验,HVALS按照您要查找的顺序返回键的值,即最早的键。现在由您决定仅使用客户端程序选择前两个值,例如HSET myhmap name "abhi"HSET myhmap email "test@test"HSET myhmap planet "earth"HSET myhmap galaxy "andromeda"HVALS myhmap将返回"abhi","test@test""earth""andromeda"