使用memcahce的时候,没有办法操作具体的key,于是自己用Awt随意写了一个
package memcache; import java.awt.Color; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.text.DefaultHighlighter; import javax.swing.text.Highlighter; import com.danga.MemCached.MemCachedClient; import com.danga.MemCached.SockIOPool; public class MemcacheUI { static JFrame frame; static JPanel panel; static JTextField urlTextField; static JButton connectBtn; static JButton closeBtn; static JTextField keyTextField; static JTextField valueTextField; static JButton getBtn; static JButton getCounterBtn; static JButton setBtn; static JButton setCounterBtn; static JButton delBtn; static JButton getAllKeysBtn; static MemcacheNode mcClient; public static void main(String[] args) { initPanel(); initEvents(); } private static void initEvents() { connectBtnEvent(); optionBtnEvent(); } private static void optionBtnEvent() { getBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (mcClient == null) { JOptionPane.showMessageDialog(null, "请先连接Memcache!"); return; } String key = keyTextField.getText().trim(); if (isNull(key)) { JOptionPane.showMessageDialog(null, "Key为空!"); return; } Object object = mcClient.get(key); setValueText(object); } }); getCounterBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (mcClient == null) { JOptionPane.showMessageDialog(null, "请先连接Memcache!"); return; } String key = keyTextField.getText().trim(); if (isNull(key)) { JOptionPane.showMessageDialog(null, "Key为空!"); return; } Long object = mcClient.getCounter(key); if (object == -1) { setValueText(null); } else { setValueText(object); } } }); setBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (mcClient == null) { JOptionPane.showMessageDialog(null, "请先连接Memcache!"); return; } String key = keyTextField.getText().trim(); String value = valueTextField.getText().trim(); if (isNull(key)) { JOptionPane.showMessageDialog(null, "Key为空!"); return; } if (isNull(value)) { JOptionPane.showMessageDialog(null, "Value为空!"); return; } mcClient.set(key, value); } }); setCounterBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (mcClient == null) { JOptionPane.showMessageDialog(null, "请先连接Memcache!"); return; } String key = keyTextField.getText().trim(); String value = valueTextField.getText().trim(); if (isNull(key)) { JOptionPane.showMessageDialog(null, "Key为空!"); return; } if (isNull(value)) { JOptionPane.showMessageDialog(null, "Value为空!"); return; } long counter = toLong(value); if (counter == -1) { JOptionPane.showMessageDialog(null, "Value必须是数字且不能是负数!"); return; } mcClient.storeCounter(key, counter); } }); delBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (mcClient == null) { JOptionPane.showMessageDialog(null, "请先连接Memcache!"); return; } String key = keyTextField.getText().trim(); if (isNull(key)) { JOptionPane.showMessageDialog(null, "Key为空!"); return; } boolean delete = mcClient.delete(key); if (delete) { JOptionPane.showMessageDialog(null, "删除成功"); } else { JOptionPane.showMessageDialog(null, "删除失败,或者没有该key"); } } }); getAllKeysBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (mcClient == null) { JOptionPane.showMessageDialog(null, "请先连接Memcache!"); return; } ArrayList<Object> allKeys = mcClient.getAllKeys(); new ShowKeyFrame(allKeys); } }); } private static boolean isNull(String s) { if (s == null || "".equals(s.trim())) { return true; } return false; } private static void setValueText(Object obj) { valueTextField.setText(obj == null ? "" : obj.toString()); } private static long toLong(String s) { long r = -1; try { r = Long.parseLong(s); if (r < 0) { return -1; } } catch (Throwable e) { return -1; } return r; } private static void connectBtnEvent() { connectBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String trim = urlTextField.getText().trim(); String[] url = trim.split(","); try { mcClient = new MemcacheNode(url); if (mcClient == null) { JOptionPane.showMessageDialog(null, "地址错误,连接不上!"); } else { connectBtn.setText("重新连接"); } } catch (Throwable ex) { JOptionPane.showMessageDialog(null, "地址错误,连接不上!"); } } }); closeBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.dispose(); } }); } private static void initPanel() { frame = new JFrame("Memcache UI, aothor: Anders"); panel = new JPanel(); frame.add(panel); frame.setSize(600, 220); frame.setLocationRelativeTo(null); frame.setResizable(false); initConnectPanel(); initOptionPanel(); show(); } private static void show() { frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private static void initConnectPanel() { JPanel connectPanel = new JPanel(); urlTextField = new JTextField(30); urlTextField.setText("127.0.0.1:11211,127.0.0.1:11211"); urlTextField.setPreferredSize(new Dimension(80, 30)); connectBtn = new JButton("连接"); closeBtn = new JButton("拜拜了"); connectPanel.add(urlTextField); connectPanel.add(connectBtn); connectPanel.add(closeBtn); panel.add(connectPanel); } private static void initOptionPanel() { JLabel keyLabel = new JLabel("Key:"); JLabel valueLabel = new JLabel("Value:"); JPanel optionPanel = new JPanel(); keyTextField = new JTextField(20); keyTextField.setPreferredSize(new Dimension(50, 30)); valueTextField = new JTextField(20); valueTextField.setPreferredSize(new Dimension(50, 30)); JPanel optionPanel2 = new JPanel(); getBtn = new JButton("Get"); getCounterBtn = new JButton("GetCounter"); setBtn = new JButton("Set"); setCounterBtn = new JButton("StoreCounter"); delBtn = new JButton("Delete"); getAllKeysBtn = new JButton("All Keys"); optionPanel.add(keyLabel); optionPanel.add(keyTextField); optionPanel.add(valueLabel); optionPanel.add(valueTextField); optionPanel2.add(getBtn); optionPanel2.add(setBtn); optionPanel2.add(getCounterBtn); optionPanel2.add(setCounterBtn); optionPanel2.add(delBtn); optionPanel2.add(getAllKeysBtn); panel.add(optionPanel); panel.add(optionPanel2); } public static class MemcacheNode { private String name; private MemCachedClient client; MemcacheNode(String[] ips) { SockIOPool pool = SockIOPool.getInstance(); pool.setServers(ips); pool.setInitConn(1); pool.setMinConn(1); pool.setMaxConn(5); pool.setMaxIdle(300000); pool.setMaxBusyTime(300000); /* 设置线程休眠时间 */ pool.setMaintSleep(10000); pool.setNagle(false);// 禁用nagle算法 pool.setSocketTO(3000);// 3秒超时 pool.setSocketConnectTO(0); pool.setAliveCheck(true); pool.setHashingAlg(0); pool.initialize(); this.client = new MemCachedClient(); } public String getName() { return name; } // ===================具体业务方法=================== public String getString(String key) { return (String) get(key); } public Object get(String key) { return this.client.get(key); } public boolean set(String key, Object value) { return this.client.set(key, value); } public boolean set(String key, Object value, Date expiry) { return this.client.set(key, value, expiry); } public boolean delete(String key) { return this.client.delete(key); } public boolean keyExists(String key) { return this.client.keyExists(key); } public boolean add(String key, Object value, Date expiry) { return this.client.add(key, value, expiry); } public Map<String, Object> getMulti(String[] keys) { return this.client.getMulti(keys); } public boolean storeCounter(String key, long counter) { return this.client.storeCounter(key, counter); } public long getCounter(String key) { return this.client.getCounter(key); } public long addOrIncr(String key, long inc) { return this.client.addOrIncr(key, inc); } public long addOrDecr(String key, long inc) { return this.client.addOrDecr(key, inc); } public long incr(String key, long inc) { return this.client.incr(key, inc); } public long incr(String key) { return this.client.incr(key); } public long decr(String key, long inc) { return this.client.decr(key, inc); } public long decr(String key) { return this.client.decr(key); } public Object[] getMultiArray(String[] keys) { return this.client.getMultiArray(keys); } public Map<String, Long> getMultiCounters(String[] keys) { Map<String, Object> rawResult = this.client.getMulti(keys, null, true); if (rawResult == null) { return null; } Map<String, Long> result = new HashMap<String, Long>(keys.length); String val; for (Entry<String, Object> en : rawResult.entrySet()) { Long counter = null; val = (String) en.getValue(); if (val != null && !val.equals("")) { counter = Long.parseLong(val.trim()); } result.put(en.getKey(), counter); } return result; } public boolean flushAll() { return this.client.flushAll(); } public boolean ping() { return !this.client.stats().isEmpty(); } public ArrayList<Object> getAllKeys() { Map statsItems = this.client.statsItems(); Set keySet = statsItems.keySet(); Map subMap = null; for (Object object : keySet) { subMap = (Map) statsItems.get(object); } HashSet<Integer> keys = new HashSet<Integer>(); for (Object object : subMap.keySet()) { String items = (String) object; String[] split = items.split(":"); if (split != null && split.length == 3) { keys.add(Integer.parseInt(split[1])); } else { continue; } } ArrayList<Object> allKeyList = new ArrayList<Object>(); for (Integer integer : keys) { Map statsCacheDump = this.client.statsCacheDump(integer, 0); for (Object object : statsCacheDump.keySet()) { String key = (String) object; Map mcKeyMap = (Map) statsCacheDump.get(key); for (Object subKey : mcKeyMap.keySet()) { allKeyList.add(subKey); } } } return allKeyList; } } public static class ShowKeyFrame { JFrame frame; JPanel panel; JTextArea textArea; JTextField searchTextField; JButton findBtn; JButton nextBtn; JButton closeBtn; ArrayList<Integer> positionList; int curPosition; public ShowKeyFrame(ArrayList<Object> keys) { frame = new JFrame(); panel = new JPanel(); textArea = new JTextArea(35, 50); findBtn = new JButton("查找"); nextBtn = new JButton("下一个"); closeBtn = new JButton("关闭"); searchTextField = new JTextField(20); searchTextField.setPreferredSize(new Dimension(50, 30)); positionList = new ArrayList<Integer>(); StringBuilder sb = new StringBuilder(); for (Object string : keys) { sb.append(string).append("\n"); } textArea.setText(sb.toString()); textArea.setEditable(false); textArea.setLineWrap(true); initCloseEvent(); frame.setSize(600, 680); JScrollPane text2 = new JScrollPane(textArea); panel.add(text2); panel.add(searchTextField); panel.add(findBtn); panel.add(nextBtn); panel.add(new JLabel("总共有" + keys.size() + " 个key ")); panel.add(closeBtn); frame.add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); } private void initCloseEvent() { closeBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.dispose(); } }); nextBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (positionList == null || positionList.isEmpty()) { return; } curPosition++; if (curPosition > (positionList.size() - 1)) { curPosition = 0; } textArea.setCaretPosition(positionList.get(curPosition)); nextBtn.setText("下一个(共" + positionList.size() + "个,第 " + (curPosition + 1) + "个)"); } }); findBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Highlighter highLighter = textArea.getHighlighter(); String text = textArea.getText(); DefaultHighlighter.DefaultHighlightPainter p = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW); String keyWord = searchTextField.getText().trim(); int pos = 0; positionList.clear(); curPosition = 0; while ((pos = text.indexOf(keyWord, pos)) >= 0) { try { positionList.add(pos); highLighter.addHighlight(pos, pos + keyWord.length(), p); pos += keyWord.length(); } catch (Exception ex) { ex.printStackTrace(); } } if (positionList.size() > 0) { curPosition = 0; textArea.setCaretPosition(positionList.get(0)); nextBtn.setText("下一个(共" + positionList.size() + "个,第 1个)"); } } }); } } }