Permalink
Browse files

init

  • Loading branch information...
sclasen committed Jul 28, 2011
0 parents commit 4c9cfc7ee2dab8b1c022879d78e26be929d37453
Showing with 189 additions and 0 deletions.
  1. +49 −0 .gitignore
  2. +1 −0 Procfile
  3. +7 −0 README.md
  4. +52 −0 pom.xml
  5. +80 −0 src/main/java/com/heroku/memcached/cached/Spy.java
@@ -0,0 +1,49 @@
+*~
+*#
+src_managed
+activemq-data
+project/plugins/project
+project/boot/*
+*/project/build/target
+*/project/boot
+lib_managed
+etags
+tags
+TAGS
+akka.tmproj
+reports
+dist
+build
+target
+deploy/*.jar
+data
+out
+logs
+.#*
+.codefellow
+storage
+.codefellow
+.ensime
+_dump
+.manager
+manifest.mf
+semantic.cache
+tm*.log
+tm*.lck
+tm.out
+*.tm.epoch
+.DS_Store
+*.iws
+*.ipr
+*.iml
+run-codefellow
+.project
+.settings
+.classpath
+.idea
+.scala_dependencies
+multiverse.log
+.eprj
+.*.swp
+akka-tutorials/akka-tutorial-pi-sbt/project/boot/
+doozer-*/dooz.log
@@ -0,0 +1 @@
+spy: sh target/bin/spy
@@ -0,0 +1,7 @@
+#Sample usage of memcached on Heroku from java using spy memcached
+
+ git clone
+ heroku create -s cedar
+ heroku addons:add memcache:5mb
+ git push heroku master
+ heroku run spy
52 pom.xml
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>com.heroku.memcached</groupId>
+ <artifactId>cached</artifactId>
+ <version>1.0</version>
+ <repositories>
+ <repository>
+ <id>spy</id>
+ <name>Spy Repository</name>
+ <url>http://files.couchbase.com/maven2/</url>
+ </repository>
+ </repositories>
+ <dependencies>
+ <dependency>
+ <groupId>spy</groupId>
+ <artifactId>spymemcached</artifactId>
+ <version>2.7</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>appassembler-maven-plugin</artifactId>
+ <version>1.1.1</version>
+ <configuration>
+ <assembleDirectory>target</assembleDirectory>
+ <generateRepository>false</generateRepository>
+ <programs>
+ <program>
+ <mainClass>com.heroku.memcached.cached.Spy</mainClass>
+ <name>spy</name>
+ </program>
+ </programs>
+ </configuration>
+ <executions>
+ <execution>
+ <phase>package</phase>
+ <goals>
+ <goal>assemble</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
@@ -0,0 +1,80 @@
+package com.heroku.memcached.cached;
+
+
+import net.spy.memcached.ConnectionFactoryBuilder;
+import net.spy.memcached.MemcachedClient;
+import net.spy.memcached.auth.AuthDescriptor;
+import net.spy.memcached.auth.PlainCallbackHandler;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.Collections;
+import java.util.concurrent.ExecutionException;
+
+public class Spy {
+
+ MemcachedClient mc;
+
+ public Spy(String server, String username, String password) {
+
+ AuthDescriptor ad = new AuthDescriptor(new String[]{"PLAIN"},
+ new PlainCallbackHandler(username, password));
+
+ try {
+ mc = new MemcachedClient(
+ new ConnectionFactoryBuilder().setProtocol(ConnectionFactoryBuilder.Protocol.BINARY)
+ .setAuthDescriptor(ad)
+ .build(), Collections.singletonList(new InetSocketAddress(server, 11211))
+ );
+
+ } catch (IOException ex) {
+ System.err.println("Couldn't create a connection, bailing out: \nIOException " + ex.getMessage());
+ }
+ }
+
+
+ public static void main(String[] args) throws ExecutionException, InterruptedException {
+ String server = System.getenv("MEMCACHE_SERVERS");
+ String username = System.getenv("MEMCACHE_USERNAME");
+ String password = System.getenv("MEMCACHE_PASSWORD");
+ Spy spy = new Spy(server, username, password);
+ spy.run();
+ }
+
+ public void run() throws ExecutionException, InterruptedException {
+ for (int i = 0; i < 1000; i++) {
+ String key = "key" + i;
+ String value = "value" + i;
+ if (mc.add(key, 0, value).get()) {
+ System.out.printf("added %s -> %s\n", key, value);
+ } else {
+ System.out.printf("ERROR ADDING %s -> %s\n", key, value);
+ }
+ }
+ for (int i = 0; i < 1000; i++) {
+ String key = "key" + i;
+ String value = "value" + (i * i);
+ if (mc.set(key, 0, value).get()) {
+ System.out.printf("set %s -> %s\n", key, value);
+ } else {
+ System.out.printf("ERROR SETTING %s -> %s\n", key, value);
+ }
+ }
+ for (int i = 0; i < 1000; i++) {
+ String key = "key" + i;
+ String value = (String) mc.get(key);
+ System.out.printf("GET %s -> %s\n", key, value);
+
+ }
+ for (int i = 0; i < 1000; i++) {
+ String key = "key" + i;
+ if (mc.delete(key).get()) {
+ System.out.printf("deleted %s\n", key);
+ } else {
+ System.out.printf("ERROR DELETING %s\n", key);
+ }
+ }
+ mc.shutdown();
+ }
+
+}

0 comments on commit 4c9cfc7

Please sign in to comment.