[
https://issues.apache.org/jira/browse/JCS-124?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14492456#comment-14492456
]
Richard Eigenmann commented on JCS-124:
---------------------------------------
Here is the unified diff:
--- intro_old.xml 2015-04-13 16:21:16.326703720 +0200
+++ intro.xml 2015-04-13 16:25:18.768268379 +0200
@@ -220,52 +220,88 @@
</p>
<source>
<![CDATA[
+import java.io.Serializable;
import org.apache.commons.jcs.JCS;
import org.apache.commons.jcs.access.CacheAccess;
import org.apache.commons.jcs.access.exception.CacheException;
-. . .
+public class JcsExample {
- private static final String cacheRegionName = "city";
+ public static void main( String[] args ) {
+ JcsExample example = new JcsExample();
+ example.testCache();
+ }
private CacheAccess<String, City> cache = null;
-. . .
- // in your constructor you might do this
- try
- {
- CacheAccess<String, City> c = JCS.getInstance(
this.getCacheRegionName() );
- setCache( c );
- }
- catch ( CacheException e )
- {
- log.error( "Problem initializing cache for region name ["
- + this.getCacheRegionName() + "].", e );
- }
-
-. . .
-
- // to get a city out of the cache by id you might do this:
- String key = "cityId:" + String.valueOf( id );
-
- City city = cache.get( key );
-
-. . .
-
- // to put a city object in the cache, you could do this:
- try
- {
- // if it isn't null, insert it
- if ( city != null )
- {
+ public JcsExample() {
+ try {
+ cache = JCS.getInstance( "default" );
+ } catch ( CacheException e ) {
+ System.out.println( String.format( "Problem initializing cache:
%s", e.getMessage() ) );
+ }
+ }
+
+ public void putInCache( City city ) {
+ String key = city.name;
+ try {
cache.put( key, city );
+ } catch ( CacheException e ) {
+ System.out.println( String.format( "Problem putting city %s in the
cache, for key %s%n%s",
+ city.name, key, e.getMessage() ) );
}
}
- catch ( CacheException e )
- {
- log.error( "Problem putting "
- + city + " in the cache, for key " + key, e );
+
+ public City retrieveFromCache( String cityKey ) {
+ return cache.get( cityKey );
}
+
+ public void testCache() {
+ City zurich = new City( "Zürich", "Switzerland", 366765 );
+ putInCache( zurich );
+
+ City berlin = new City( "Berlin", "Germany", 3502000 );
+ putInCache( berlin );
+
+ City johannesburg = new City( "Johannesburg", "South Africa", 12200000
);
+ putInCache( johannesburg );
+
+ City retrievedCity1 = retrieveFromCache( "Berlin" );
+ if ( retrievedCity1 != null ) {
+ System.out.println( retrievedCity1.toString() );
+ } else {
+ System.out.println( "No object was found in the cache for the key
\"Berlin\"" );
+ }
+
+ City retrievedCity2 = retrieveFromCache( "New York" );
+ if ( retrievedCity2 != null ) {
+ System.out.println( retrievedCity2.toString() );
+ } else {
+ System.out.println( "No object was found in the cache for the key
\"New York\"" );
+ }
+ }
+
+ // defined as a nested inner class to reduce number of .java files in the
example
+ public class City implements Serializable {
+
+ private static final long serialVersionUID = 6392376146163510146L;
+ public String name;
+ public String country;
+ public int population;
+
+ public City( String name, String country, int population ) {
+ this.name = name;
+ this.country = country;
+ this.population = population;
+ }
+
+ @Override
+ public String toString() {
+ return String.format( "%s is a city in the country %s with a
population of %d", name, country, population );
+ }
+ }
+}
+
]]>
</source>
</section>
> Make the code in Step 5 on the JCS overview page a full working class that
> can compile
> --------------------------------------------------------------------------------------
>
> Key: JCS-124
> URL: https://issues.apache.org/jira/browse/JCS-124
> Project: Commons JCS
> Issue Type: Improvement
> Components: Documentation
> Affects Versions: jcs-2.0-beta-1
> Environment: Testing
> Reporter: Richard Eigenmann
> Fix For: jcs-2.0-beta-2
>
> Attachments: intro.xml
>
>
> Step 5:
> http://commons.apache.org/proper/commons-jcs/getting_started/intro.html
> Please make this a fully compilable class that can be cut and pasted for a
> quick success experience for the newby.
> Defaults and examples are extremely powerful and many people never venture
> further than the defaults and samples they discovered so it is extremely
> important to show sensible default values and good code.
> In this vein, drop the words "might" and "could" in the comments. It's either
> a good way to do it or you should not be showing it for it will be copied.
> Please also correct the setCache(c); instruction which doesn't make sense.
> The example doesn't indicate that it is extending another class where
> setCache(c) might be defined and isn't defining the method. Is this
> instruction needed?
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)